messenger.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240
  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_connect_retry(struct ceph_connection *con)
  729. {
  730. dout("prepare_read_connect_retry %p\n", con);
  731. con->in_base_pos = strlen(CEPH_BANNER) + sizeof(con->actual_peer_addr)
  732. + sizeof(con->peer_addr_for_me);
  733. }
  734. static void prepare_read_ack(struct ceph_connection *con)
  735. {
  736. dout("prepare_read_ack %p\n", con);
  737. con->in_base_pos = 0;
  738. }
  739. static void prepare_read_tag(struct ceph_connection *con)
  740. {
  741. dout("prepare_read_tag %p\n", con);
  742. con->in_base_pos = 0;
  743. con->in_tag = CEPH_MSGR_TAG_READY;
  744. }
  745. /*
  746. * Prepare to read a message.
  747. */
  748. static int prepare_read_message(struct ceph_connection *con)
  749. {
  750. dout("prepare_read_message %p\n", con);
  751. BUG_ON(con->in_msg != NULL);
  752. con->in_base_pos = 0;
  753. con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
  754. return 0;
  755. }
  756. static int read_partial(struct ceph_connection *con,
  757. int *to, int size, void *object)
  758. {
  759. *to += size;
  760. while (con->in_base_pos < *to) {
  761. int left = *to - con->in_base_pos;
  762. int have = size - left;
  763. int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
  764. if (ret <= 0)
  765. return ret;
  766. con->in_base_pos += ret;
  767. }
  768. return 1;
  769. }
  770. /*
  771. * Read all or part of the connect-side handshake on a new connection
  772. */
  773. static int read_partial_banner(struct ceph_connection *con)
  774. {
  775. int ret, to = 0;
  776. dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
  777. /* peer's banner */
  778. ret = read_partial(con, &to, strlen(CEPH_BANNER), con->in_banner);
  779. if (ret <= 0)
  780. goto out;
  781. ret = read_partial(con, &to, sizeof(con->actual_peer_addr),
  782. &con->actual_peer_addr);
  783. if (ret <= 0)
  784. goto out;
  785. ret = read_partial(con, &to, sizeof(con->peer_addr_for_me),
  786. &con->peer_addr_for_me);
  787. if (ret <= 0)
  788. goto out;
  789. out:
  790. return ret;
  791. }
  792. static int read_partial_connect(struct ceph_connection *con)
  793. {
  794. int ret, to = 0;
  795. dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
  796. ret = read_partial(con, &to, sizeof(con->in_reply), &con->in_reply);
  797. if (ret <= 0)
  798. goto out;
  799. ret = read_partial(con, &to, le32_to_cpu(con->in_reply.authorizer_len),
  800. con->auth_reply_buf);
  801. if (ret <= 0)
  802. goto out;
  803. dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
  804. con, (int)con->in_reply.tag,
  805. le32_to_cpu(con->in_reply.connect_seq),
  806. le32_to_cpu(con->in_reply.global_seq));
  807. out:
  808. return ret;
  809. }
  810. /*
  811. * Verify the hello banner looks okay.
  812. */
  813. static int verify_hello(struct ceph_connection *con)
  814. {
  815. if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
  816. pr_err("connect to %s got bad banner\n",
  817. pr_addr(&con->peer_addr.in_addr));
  818. con->error_msg = "protocol error, bad banner";
  819. return -1;
  820. }
  821. return 0;
  822. }
  823. static bool addr_is_blank(struct sockaddr_storage *ss)
  824. {
  825. switch (ss->ss_family) {
  826. case AF_INET:
  827. return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
  828. case AF_INET6:
  829. return
  830. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
  831. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
  832. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
  833. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
  834. }
  835. return false;
  836. }
  837. static int addr_port(struct sockaddr_storage *ss)
  838. {
  839. switch (ss->ss_family) {
  840. case AF_INET:
  841. return ntohs(((struct sockaddr_in *)ss)->sin_port);
  842. case AF_INET6:
  843. return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
  844. }
  845. return 0;
  846. }
  847. static void addr_set_port(struct sockaddr_storage *ss, int p)
  848. {
  849. switch (ss->ss_family) {
  850. case AF_INET:
  851. ((struct sockaddr_in *)ss)->sin_port = htons(p);
  852. case AF_INET6:
  853. ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
  854. }
  855. }
  856. /*
  857. * Parse an ip[:port] list into an addr array. Use the default
  858. * monitor port if a port isn't specified.
  859. */
  860. int ceph_parse_ips(const char *c, const char *end,
  861. struct ceph_entity_addr *addr,
  862. int max_count, int *count)
  863. {
  864. int i;
  865. const char *p = c;
  866. dout("parse_ips on '%.*s'\n", (int)(end-c), c);
  867. for (i = 0; i < max_count; i++) {
  868. const char *ipend;
  869. struct sockaddr_storage *ss = &addr[i].in_addr;
  870. struct sockaddr_in *in4 = (void *)ss;
  871. struct sockaddr_in6 *in6 = (void *)ss;
  872. int port;
  873. memset(ss, 0, sizeof(*ss));
  874. if (in4_pton(p, end - p, (u8 *)&in4->sin_addr.s_addr,
  875. ',', &ipend)) {
  876. ss->ss_family = AF_INET;
  877. } else if (in6_pton(p, end - p, (u8 *)&in6->sin6_addr.s6_addr,
  878. ',', &ipend)) {
  879. ss->ss_family = AF_INET6;
  880. } else {
  881. goto bad;
  882. }
  883. p = ipend;
  884. /* port? */
  885. if (p < end && *p == ':') {
  886. port = 0;
  887. p++;
  888. while (p < end && *p >= '0' && *p <= '9') {
  889. port = (port * 10) + (*p - '0');
  890. p++;
  891. }
  892. if (port > 65535 || port == 0)
  893. goto bad;
  894. } else {
  895. port = CEPH_MON_PORT;
  896. }
  897. addr_set_port(ss, port);
  898. dout("parse_ips got %s\n", pr_addr(ss));
  899. if (p == end)
  900. break;
  901. if (*p != ',')
  902. goto bad;
  903. p++;
  904. }
  905. if (p != end)
  906. goto bad;
  907. if (count)
  908. *count = i + 1;
  909. return 0;
  910. bad:
  911. pr_err("parse_ips bad ip '%s'\n", c);
  912. return -EINVAL;
  913. }
  914. static int process_banner(struct ceph_connection *con)
  915. {
  916. dout("process_banner on %p\n", con);
  917. if (verify_hello(con) < 0)
  918. return -1;
  919. ceph_decode_addr(&con->actual_peer_addr);
  920. ceph_decode_addr(&con->peer_addr_for_me);
  921. /*
  922. * Make sure the other end is who we wanted. note that the other
  923. * end may not yet know their ip address, so if it's 0.0.0.0, give
  924. * them the benefit of the doubt.
  925. */
  926. if (memcmp(&con->peer_addr, &con->actual_peer_addr,
  927. sizeof(con->peer_addr)) != 0 &&
  928. !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
  929. con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
  930. pr_warning("wrong peer, want %s/%lld, got %s/%lld\n",
  931. pr_addr(&con->peer_addr.in_addr),
  932. le64_to_cpu(con->peer_addr.nonce),
  933. pr_addr(&con->actual_peer_addr.in_addr),
  934. le64_to_cpu(con->actual_peer_addr.nonce));
  935. con->error_msg = "wrong peer at address";
  936. return -1;
  937. }
  938. /*
  939. * did we learn our address?
  940. */
  941. if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
  942. int port = addr_port(&con->msgr->inst.addr.in_addr);
  943. memcpy(&con->msgr->inst.addr.in_addr,
  944. &con->peer_addr_for_me.in_addr,
  945. sizeof(con->peer_addr_for_me.in_addr));
  946. addr_set_port(&con->msgr->inst.addr.in_addr, port);
  947. encode_my_addr(con->msgr);
  948. dout("process_banner learned my addr is %s\n",
  949. pr_addr(&con->msgr->inst.addr.in_addr));
  950. }
  951. set_bit(NEGOTIATING, &con->state);
  952. prepare_read_connect(con);
  953. return 0;
  954. }
  955. static void fail_protocol(struct ceph_connection *con)
  956. {
  957. reset_connection(con);
  958. set_bit(CLOSED, &con->state); /* in case there's queued work */
  959. mutex_unlock(&con->mutex);
  960. if (con->ops->bad_proto)
  961. con->ops->bad_proto(con);
  962. mutex_lock(&con->mutex);
  963. }
  964. static int process_connect(struct ceph_connection *con)
  965. {
  966. u64 sup_feat = CEPH_FEATURE_SUPPORTED;
  967. u64 req_feat = CEPH_FEATURE_REQUIRED;
  968. u64 server_feat = le64_to_cpu(con->in_reply.features);
  969. dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
  970. switch (con->in_reply.tag) {
  971. case CEPH_MSGR_TAG_FEATURES:
  972. pr_err("%s%lld %s feature set mismatch,"
  973. " my %llx < server's %llx, missing %llx\n",
  974. ENTITY_NAME(con->peer_name),
  975. pr_addr(&con->peer_addr.in_addr),
  976. sup_feat, server_feat, server_feat & ~sup_feat);
  977. con->error_msg = "missing required protocol features";
  978. fail_protocol(con);
  979. return -1;
  980. case CEPH_MSGR_TAG_BADPROTOVER:
  981. pr_err("%s%lld %s protocol version mismatch,"
  982. " my %d != server's %d\n",
  983. ENTITY_NAME(con->peer_name),
  984. pr_addr(&con->peer_addr.in_addr),
  985. le32_to_cpu(con->out_connect.protocol_version),
  986. le32_to_cpu(con->in_reply.protocol_version));
  987. con->error_msg = "protocol version mismatch";
  988. fail_protocol(con);
  989. return -1;
  990. case CEPH_MSGR_TAG_BADAUTHORIZER:
  991. con->auth_retry++;
  992. dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
  993. con->auth_retry);
  994. if (con->auth_retry == 2) {
  995. con->error_msg = "connect authorization failure";
  996. reset_connection(con);
  997. set_bit(CLOSED, &con->state);
  998. return -1;
  999. }
  1000. con->auth_retry = 1;
  1001. prepare_write_connect(con->msgr, con, 0);
  1002. prepare_read_connect_retry(con);
  1003. break;
  1004. case CEPH_MSGR_TAG_RESETSESSION:
  1005. /*
  1006. * If we connected with a large connect_seq but the peer
  1007. * has no record of a session with us (no connection, or
  1008. * connect_seq == 0), they will send RESETSESION to indicate
  1009. * that they must have reset their session, and may have
  1010. * dropped messages.
  1011. */
  1012. dout("process_connect got RESET peer seq %u\n",
  1013. le32_to_cpu(con->in_connect.connect_seq));
  1014. pr_err("%s%lld %s connection reset\n",
  1015. ENTITY_NAME(con->peer_name),
  1016. pr_addr(&con->peer_addr.in_addr));
  1017. reset_connection(con);
  1018. prepare_write_connect(con->msgr, con, 0);
  1019. prepare_read_connect(con);
  1020. /* Tell ceph about it. */
  1021. mutex_unlock(&con->mutex);
  1022. pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
  1023. if (con->ops->peer_reset)
  1024. con->ops->peer_reset(con);
  1025. mutex_lock(&con->mutex);
  1026. break;
  1027. case CEPH_MSGR_TAG_RETRY_SESSION:
  1028. /*
  1029. * If we sent a smaller connect_seq than the peer has, try
  1030. * again with a larger value.
  1031. */
  1032. dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
  1033. le32_to_cpu(con->out_connect.connect_seq),
  1034. le32_to_cpu(con->in_connect.connect_seq));
  1035. con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
  1036. prepare_write_connect(con->msgr, con, 0);
  1037. prepare_read_connect(con);
  1038. break;
  1039. case CEPH_MSGR_TAG_RETRY_GLOBAL:
  1040. /*
  1041. * If we sent a smaller global_seq than the peer has, try
  1042. * again with a larger value.
  1043. */
  1044. dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
  1045. con->peer_global_seq,
  1046. le32_to_cpu(con->in_connect.global_seq));
  1047. get_global_seq(con->msgr,
  1048. le32_to_cpu(con->in_connect.global_seq));
  1049. prepare_write_connect(con->msgr, con, 0);
  1050. prepare_read_connect(con);
  1051. break;
  1052. case CEPH_MSGR_TAG_READY:
  1053. if (req_feat & ~server_feat) {
  1054. pr_err("%s%lld %s protocol feature mismatch,"
  1055. " my required %llx > server's %llx, need %llx\n",
  1056. ENTITY_NAME(con->peer_name),
  1057. pr_addr(&con->peer_addr.in_addr),
  1058. req_feat, server_feat, req_feat & ~server_feat);
  1059. con->error_msg = "missing required protocol features";
  1060. fail_protocol(con);
  1061. return -1;
  1062. }
  1063. clear_bit(CONNECTING, &con->state);
  1064. con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
  1065. con->connect_seq++;
  1066. dout("process_connect got READY gseq %d cseq %d (%d)\n",
  1067. con->peer_global_seq,
  1068. le32_to_cpu(con->in_reply.connect_seq),
  1069. con->connect_seq);
  1070. WARN_ON(con->connect_seq !=
  1071. le32_to_cpu(con->in_reply.connect_seq));
  1072. if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
  1073. set_bit(LOSSYTX, &con->state);
  1074. prepare_read_tag(con);
  1075. break;
  1076. case CEPH_MSGR_TAG_WAIT:
  1077. /*
  1078. * If there is a connection race (we are opening
  1079. * connections to each other), one of us may just have
  1080. * to WAIT. This shouldn't happen if we are the
  1081. * client.
  1082. */
  1083. pr_err("process_connect peer connecting WAIT\n");
  1084. default:
  1085. pr_err("connect protocol error, will retry\n");
  1086. con->error_msg = "protocol error, garbage tag during connect";
  1087. return -1;
  1088. }
  1089. return 0;
  1090. }
  1091. /*
  1092. * read (part of) an ack
  1093. */
  1094. static int read_partial_ack(struct ceph_connection *con)
  1095. {
  1096. int to = 0;
  1097. return read_partial(con, &to, sizeof(con->in_temp_ack),
  1098. &con->in_temp_ack);
  1099. }
  1100. /*
  1101. * We can finally discard anything that's been acked.
  1102. */
  1103. static void process_ack(struct ceph_connection *con)
  1104. {
  1105. struct ceph_msg *m;
  1106. u64 ack = le64_to_cpu(con->in_temp_ack);
  1107. u64 seq;
  1108. while (!list_empty(&con->out_sent)) {
  1109. m = list_first_entry(&con->out_sent, struct ceph_msg,
  1110. list_head);
  1111. seq = le64_to_cpu(m->hdr.seq);
  1112. if (seq > ack)
  1113. break;
  1114. dout("got ack for seq %llu type %d at %p\n", seq,
  1115. le16_to_cpu(m->hdr.type), m);
  1116. ceph_msg_remove(m);
  1117. }
  1118. prepare_read_tag(con);
  1119. }
  1120. static int read_partial_message_section(struct ceph_connection *con,
  1121. struct kvec *section, unsigned int sec_len,
  1122. u32 *crc)
  1123. {
  1124. int left;
  1125. int ret;
  1126. BUG_ON(!section);
  1127. while (section->iov_len < sec_len) {
  1128. BUG_ON(section->iov_base == NULL);
  1129. left = sec_len - section->iov_len;
  1130. ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
  1131. section->iov_len, left);
  1132. if (ret <= 0)
  1133. return ret;
  1134. section->iov_len += ret;
  1135. if (section->iov_len == sec_len)
  1136. *crc = crc32c(0, section->iov_base,
  1137. section->iov_len);
  1138. }
  1139. return 1;
  1140. }
  1141. static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
  1142. struct ceph_msg_header *hdr,
  1143. int *skip);
  1144. /*
  1145. * read (part of) a message.
  1146. */
  1147. static int read_partial_message(struct ceph_connection *con)
  1148. {
  1149. struct ceph_msg *m = con->in_msg;
  1150. void *p;
  1151. int ret;
  1152. int to, left;
  1153. unsigned front_len, middle_len, data_len, data_off;
  1154. int datacrc = con->msgr->nocrc;
  1155. int skip;
  1156. dout("read_partial_message con %p msg %p\n", con, m);
  1157. /* header */
  1158. while (con->in_base_pos < sizeof(con->in_hdr)) {
  1159. left = sizeof(con->in_hdr) - con->in_base_pos;
  1160. ret = ceph_tcp_recvmsg(con->sock,
  1161. (char *)&con->in_hdr + con->in_base_pos,
  1162. left);
  1163. if (ret <= 0)
  1164. return ret;
  1165. con->in_base_pos += ret;
  1166. if (con->in_base_pos == sizeof(con->in_hdr)) {
  1167. u32 crc = crc32c(0, (void *)&con->in_hdr,
  1168. sizeof(con->in_hdr) - sizeof(con->in_hdr.crc));
  1169. if (crc != le32_to_cpu(con->in_hdr.crc)) {
  1170. pr_err("read_partial_message bad hdr "
  1171. " crc %u != expected %u\n",
  1172. crc, con->in_hdr.crc);
  1173. return -EBADMSG;
  1174. }
  1175. }
  1176. }
  1177. front_len = le32_to_cpu(con->in_hdr.front_len);
  1178. if (front_len > CEPH_MSG_MAX_FRONT_LEN)
  1179. return -EIO;
  1180. middle_len = le32_to_cpu(con->in_hdr.middle_len);
  1181. if (middle_len > CEPH_MSG_MAX_DATA_LEN)
  1182. return -EIO;
  1183. data_len = le32_to_cpu(con->in_hdr.data_len);
  1184. if (data_len > CEPH_MSG_MAX_DATA_LEN)
  1185. return -EIO;
  1186. data_off = le16_to_cpu(con->in_hdr.data_off);
  1187. /* allocate message? */
  1188. if (!con->in_msg) {
  1189. dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
  1190. con->in_hdr.front_len, con->in_hdr.data_len);
  1191. con->in_msg = ceph_alloc_msg(con, &con->in_hdr, &skip);
  1192. if (skip) {
  1193. /* skip this message */
  1194. dout("alloc_msg returned NULL, skipping message\n");
  1195. con->in_base_pos = -front_len - middle_len - data_len -
  1196. sizeof(m->footer);
  1197. con->in_tag = CEPH_MSGR_TAG_READY;
  1198. return 0;
  1199. }
  1200. if (IS_ERR(con->in_msg)) {
  1201. ret = PTR_ERR(con->in_msg);
  1202. con->in_msg = NULL;
  1203. con->error_msg =
  1204. "error allocating memory for incoming message";
  1205. return ret;
  1206. }
  1207. m = con->in_msg;
  1208. m->front.iov_len = 0; /* haven't read it yet */
  1209. if (m->middle)
  1210. m->middle->vec.iov_len = 0;
  1211. con->in_msg_pos.page = 0;
  1212. con->in_msg_pos.page_pos = data_off & ~PAGE_MASK;
  1213. con->in_msg_pos.data_pos = 0;
  1214. }
  1215. /* front */
  1216. ret = read_partial_message_section(con, &m->front, front_len,
  1217. &con->in_front_crc);
  1218. if (ret <= 0)
  1219. return ret;
  1220. /* middle */
  1221. if (m->middle) {
  1222. ret = read_partial_message_section(con, &m->middle->vec, middle_len,
  1223. &con->in_middle_crc);
  1224. if (ret <= 0)
  1225. return ret;
  1226. }
  1227. /* (page) data */
  1228. while (con->in_msg_pos.data_pos < data_len) {
  1229. left = min((int)(data_len - con->in_msg_pos.data_pos),
  1230. (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
  1231. BUG_ON(m->pages == NULL);
  1232. p = kmap(m->pages[con->in_msg_pos.page]);
  1233. ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
  1234. left);
  1235. if (ret > 0 && datacrc)
  1236. con->in_data_crc =
  1237. crc32c(con->in_data_crc,
  1238. p + con->in_msg_pos.page_pos, ret);
  1239. kunmap(m->pages[con->in_msg_pos.page]);
  1240. if (ret <= 0)
  1241. return ret;
  1242. con->in_msg_pos.data_pos += ret;
  1243. con->in_msg_pos.page_pos += ret;
  1244. if (con->in_msg_pos.page_pos == PAGE_SIZE) {
  1245. con->in_msg_pos.page_pos = 0;
  1246. con->in_msg_pos.page++;
  1247. }
  1248. }
  1249. /* footer */
  1250. to = sizeof(m->hdr) + sizeof(m->footer);
  1251. while (con->in_base_pos < to) {
  1252. left = to - con->in_base_pos;
  1253. ret = ceph_tcp_recvmsg(con->sock, (char *)&m->footer +
  1254. (con->in_base_pos - sizeof(m->hdr)),
  1255. left);
  1256. if (ret <= 0)
  1257. return ret;
  1258. con->in_base_pos += ret;
  1259. }
  1260. dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
  1261. m, front_len, m->footer.front_crc, middle_len,
  1262. m->footer.middle_crc, data_len, m->footer.data_crc);
  1263. /* crc ok? */
  1264. if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
  1265. pr_err("read_partial_message %p front crc %u != exp. %u\n",
  1266. m, con->in_front_crc, m->footer.front_crc);
  1267. return -EBADMSG;
  1268. }
  1269. if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
  1270. pr_err("read_partial_message %p middle crc %u != exp %u\n",
  1271. m, con->in_middle_crc, m->footer.middle_crc);
  1272. return -EBADMSG;
  1273. }
  1274. if (datacrc &&
  1275. (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
  1276. con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
  1277. pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
  1278. con->in_data_crc, le32_to_cpu(m->footer.data_crc));
  1279. return -EBADMSG;
  1280. }
  1281. return 1; /* done! */
  1282. }
  1283. /*
  1284. * Process message. This happens in the worker thread. The callback should
  1285. * be careful not to do anything that waits on other incoming messages or it
  1286. * may deadlock.
  1287. */
  1288. static void process_message(struct ceph_connection *con)
  1289. {
  1290. struct ceph_msg *msg;
  1291. msg = con->in_msg;
  1292. con->in_msg = NULL;
  1293. /* if first message, set peer_name */
  1294. if (con->peer_name.type == 0)
  1295. con->peer_name = msg->hdr.src.name;
  1296. con->in_seq++;
  1297. mutex_unlock(&con->mutex);
  1298. dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
  1299. msg, le64_to_cpu(msg->hdr.seq),
  1300. ENTITY_NAME(msg->hdr.src.name),
  1301. le16_to_cpu(msg->hdr.type),
  1302. ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
  1303. le32_to_cpu(msg->hdr.front_len),
  1304. le32_to_cpu(msg->hdr.data_len),
  1305. con->in_front_crc, con->in_middle_crc, con->in_data_crc);
  1306. con->ops->dispatch(con, msg);
  1307. mutex_lock(&con->mutex);
  1308. prepare_read_tag(con);
  1309. }
  1310. /*
  1311. * Write something to the socket. Called in a worker thread when the
  1312. * socket appears to be writeable and we have something ready to send.
  1313. */
  1314. static int try_write(struct ceph_connection *con)
  1315. {
  1316. struct ceph_messenger *msgr = con->msgr;
  1317. int ret = 1;
  1318. dout("try_write start %p state %lu nref %d\n", con, con->state,
  1319. atomic_read(&con->nref));
  1320. mutex_lock(&con->mutex);
  1321. more:
  1322. dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
  1323. /* open the socket first? */
  1324. if (con->sock == NULL) {
  1325. /*
  1326. * if we were STANDBY and are reconnecting _this_
  1327. * connection, bump connect_seq now. Always bump
  1328. * global_seq.
  1329. */
  1330. if (test_and_clear_bit(STANDBY, &con->state))
  1331. con->connect_seq++;
  1332. prepare_write_banner(msgr, con);
  1333. prepare_write_connect(msgr, con, 1);
  1334. prepare_read_banner(con);
  1335. set_bit(CONNECTING, &con->state);
  1336. clear_bit(NEGOTIATING, &con->state);
  1337. BUG_ON(con->in_msg);
  1338. con->in_tag = CEPH_MSGR_TAG_READY;
  1339. dout("try_write initiating connect on %p new state %lu\n",
  1340. con, con->state);
  1341. con->sock = ceph_tcp_connect(con);
  1342. if (IS_ERR(con->sock)) {
  1343. con->sock = NULL;
  1344. con->error_msg = "connect error";
  1345. ret = -1;
  1346. goto out;
  1347. }
  1348. }
  1349. more_kvec:
  1350. /* kvec data queued? */
  1351. if (con->out_skip) {
  1352. ret = write_partial_skip(con);
  1353. if (ret <= 0)
  1354. goto done;
  1355. if (ret < 0) {
  1356. dout("try_write write_partial_skip err %d\n", ret);
  1357. goto done;
  1358. }
  1359. }
  1360. if (con->out_kvec_left) {
  1361. ret = write_partial_kvec(con);
  1362. if (ret <= 0)
  1363. goto done;
  1364. }
  1365. /* msg pages? */
  1366. if (con->out_msg) {
  1367. if (con->out_msg_done) {
  1368. ceph_msg_put(con->out_msg);
  1369. con->out_msg = NULL; /* we're done with this one */
  1370. goto do_next;
  1371. }
  1372. ret = write_partial_msg_pages(con);
  1373. if (ret == 1)
  1374. goto more_kvec; /* we need to send the footer, too! */
  1375. if (ret == 0)
  1376. goto done;
  1377. if (ret < 0) {
  1378. dout("try_write write_partial_msg_pages err %d\n",
  1379. ret);
  1380. goto done;
  1381. }
  1382. }
  1383. do_next:
  1384. if (!test_bit(CONNECTING, &con->state)) {
  1385. /* is anything else pending? */
  1386. if (!list_empty(&con->out_queue)) {
  1387. prepare_write_message(con);
  1388. goto more;
  1389. }
  1390. if (con->in_seq > con->in_seq_acked) {
  1391. prepare_write_ack(con);
  1392. goto more;
  1393. }
  1394. if (test_and_clear_bit(KEEPALIVE_PENDING, &con->state)) {
  1395. prepare_write_keepalive(con);
  1396. goto more;
  1397. }
  1398. }
  1399. /* Nothing to do! */
  1400. clear_bit(WRITE_PENDING, &con->state);
  1401. dout("try_write nothing else to write.\n");
  1402. done:
  1403. ret = 0;
  1404. out:
  1405. mutex_unlock(&con->mutex);
  1406. dout("try_write done on %p\n", con);
  1407. return ret;
  1408. }
  1409. /*
  1410. * Read what we can from the socket.
  1411. */
  1412. static int try_read(struct ceph_connection *con)
  1413. {
  1414. struct ceph_messenger *msgr;
  1415. int ret = -1;
  1416. if (!con->sock)
  1417. return 0;
  1418. if (test_bit(STANDBY, &con->state))
  1419. return 0;
  1420. dout("try_read start on %p\n", con);
  1421. msgr = con->msgr;
  1422. mutex_lock(&con->mutex);
  1423. more:
  1424. dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
  1425. con->in_base_pos);
  1426. if (test_bit(CONNECTING, &con->state)) {
  1427. if (!test_bit(NEGOTIATING, &con->state)) {
  1428. dout("try_read connecting\n");
  1429. ret = read_partial_banner(con);
  1430. if (ret <= 0)
  1431. goto done;
  1432. if (process_banner(con) < 0) {
  1433. ret = -1;
  1434. goto out;
  1435. }
  1436. }
  1437. ret = read_partial_connect(con);
  1438. if (ret <= 0)
  1439. goto done;
  1440. if (process_connect(con) < 0) {
  1441. ret = -1;
  1442. goto out;
  1443. }
  1444. goto more;
  1445. }
  1446. if (con->in_base_pos < 0) {
  1447. /*
  1448. * skipping + discarding content.
  1449. *
  1450. * FIXME: there must be a better way to do this!
  1451. */
  1452. static char buf[1024];
  1453. int skip = min(1024, -con->in_base_pos);
  1454. dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
  1455. ret = ceph_tcp_recvmsg(con->sock, buf, skip);
  1456. if (ret <= 0)
  1457. goto done;
  1458. con->in_base_pos += ret;
  1459. if (con->in_base_pos)
  1460. goto more;
  1461. }
  1462. if (con->in_tag == CEPH_MSGR_TAG_READY) {
  1463. /*
  1464. * what's next?
  1465. */
  1466. ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
  1467. if (ret <= 0)
  1468. goto done;
  1469. dout("try_read got tag %d\n", (int)con->in_tag);
  1470. switch (con->in_tag) {
  1471. case CEPH_MSGR_TAG_MSG:
  1472. prepare_read_message(con);
  1473. break;
  1474. case CEPH_MSGR_TAG_ACK:
  1475. prepare_read_ack(con);
  1476. break;
  1477. case CEPH_MSGR_TAG_CLOSE:
  1478. set_bit(CLOSED, &con->state); /* fixme */
  1479. goto done;
  1480. default:
  1481. goto bad_tag;
  1482. }
  1483. }
  1484. if (con->in_tag == CEPH_MSGR_TAG_MSG) {
  1485. ret = read_partial_message(con);
  1486. if (ret <= 0) {
  1487. switch (ret) {
  1488. case -EBADMSG:
  1489. con->error_msg = "bad crc";
  1490. ret = -EIO;
  1491. goto out;
  1492. case -EIO:
  1493. con->error_msg = "io error";
  1494. goto out;
  1495. default:
  1496. goto done;
  1497. }
  1498. }
  1499. if (con->in_tag == CEPH_MSGR_TAG_READY)
  1500. goto more;
  1501. process_message(con);
  1502. goto more;
  1503. }
  1504. if (con->in_tag == CEPH_MSGR_TAG_ACK) {
  1505. ret = read_partial_ack(con);
  1506. if (ret <= 0)
  1507. goto done;
  1508. process_ack(con);
  1509. goto more;
  1510. }
  1511. done:
  1512. ret = 0;
  1513. out:
  1514. mutex_unlock(&con->mutex);
  1515. dout("try_read done on %p\n", con);
  1516. return ret;
  1517. bad_tag:
  1518. pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
  1519. con->error_msg = "protocol error, garbage tag";
  1520. ret = -1;
  1521. goto out;
  1522. }
  1523. /*
  1524. * Atomically queue work on a connection. Bump @con reference to
  1525. * avoid races with connection teardown.
  1526. *
  1527. * There is some trickery going on with QUEUED and BUSY because we
  1528. * only want a _single_ thread operating on each connection at any
  1529. * point in time, but we want to use all available CPUs.
  1530. *
  1531. * The worker thread only proceeds if it can atomically set BUSY. It
  1532. * clears QUEUED and does it's thing. When it thinks it's done, it
  1533. * clears BUSY, then rechecks QUEUED.. if it's set again, it loops
  1534. * (tries again to set BUSY).
  1535. *
  1536. * To queue work, we first set QUEUED, _then_ if BUSY isn't set, we
  1537. * try to queue work. If that fails (work is already queued, or BUSY)
  1538. * we give up (work also already being done or is queued) but leave QUEUED
  1539. * set so that the worker thread will loop if necessary.
  1540. */
  1541. static void queue_con(struct ceph_connection *con)
  1542. {
  1543. if (test_bit(DEAD, &con->state)) {
  1544. dout("queue_con %p ignoring: DEAD\n",
  1545. con);
  1546. return;
  1547. }
  1548. if (!con->ops->get(con)) {
  1549. dout("queue_con %p ref count 0\n", con);
  1550. return;
  1551. }
  1552. set_bit(QUEUED, &con->state);
  1553. if (test_bit(BUSY, &con->state)) {
  1554. dout("queue_con %p - already BUSY\n", con);
  1555. con->ops->put(con);
  1556. } else if (!queue_work(ceph_msgr_wq, &con->work.work)) {
  1557. dout("queue_con %p - already queued\n", con);
  1558. con->ops->put(con);
  1559. } else {
  1560. dout("queue_con %p\n", con);
  1561. }
  1562. }
  1563. /*
  1564. * Do some work on a connection. Drop a connection ref when we're done.
  1565. */
  1566. static void con_work(struct work_struct *work)
  1567. {
  1568. struct ceph_connection *con = container_of(work, struct ceph_connection,
  1569. work.work);
  1570. int backoff = 0;
  1571. more:
  1572. if (test_and_set_bit(BUSY, &con->state) != 0) {
  1573. dout("con_work %p BUSY already set\n", con);
  1574. goto out;
  1575. }
  1576. dout("con_work %p start, clearing QUEUED\n", con);
  1577. clear_bit(QUEUED, &con->state);
  1578. if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
  1579. dout("con_work CLOSED\n");
  1580. con_close_socket(con);
  1581. goto done;
  1582. }
  1583. if (test_and_clear_bit(OPENING, &con->state)) {
  1584. /* reopen w/ new peer */
  1585. dout("con_work OPENING\n");
  1586. con_close_socket(con);
  1587. }
  1588. if (test_and_clear_bit(SOCK_CLOSED, &con->state) ||
  1589. try_read(con) < 0 ||
  1590. try_write(con) < 0) {
  1591. backoff = 1;
  1592. ceph_fault(con); /* error/fault path */
  1593. }
  1594. done:
  1595. clear_bit(BUSY, &con->state);
  1596. dout("con->state=%lu\n", con->state);
  1597. if (test_bit(QUEUED, &con->state)) {
  1598. if (!backoff || test_bit(OPENING, &con->state)) {
  1599. dout("con_work %p QUEUED reset, looping\n", con);
  1600. goto more;
  1601. }
  1602. dout("con_work %p QUEUED reset, but just faulted\n", con);
  1603. clear_bit(QUEUED, &con->state);
  1604. }
  1605. dout("con_work %p done\n", con);
  1606. out:
  1607. con->ops->put(con);
  1608. }
  1609. /*
  1610. * Generic error/fault handler. A retry mechanism is used with
  1611. * exponential backoff
  1612. */
  1613. static void ceph_fault(struct ceph_connection *con)
  1614. {
  1615. pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
  1616. pr_addr(&con->peer_addr.in_addr), con->error_msg);
  1617. dout("fault %p state %lu to peer %s\n",
  1618. con, con->state, pr_addr(&con->peer_addr.in_addr));
  1619. if (test_bit(LOSSYTX, &con->state)) {
  1620. dout("fault on LOSSYTX channel\n");
  1621. goto out;
  1622. }
  1623. clear_bit(BUSY, &con->state); /* to avoid an improbable race */
  1624. mutex_lock(&con->mutex);
  1625. if (test_bit(CLOSED, &con->state))
  1626. goto out_unlock;
  1627. con_close_socket(con);
  1628. if (con->in_msg) {
  1629. ceph_msg_put(con->in_msg);
  1630. con->in_msg = NULL;
  1631. }
  1632. /* Requeue anything that hasn't been acked */
  1633. list_splice_init(&con->out_sent, &con->out_queue);
  1634. /* If there are no messages in the queue, place the connection
  1635. * in a STANDBY state (i.e., don't try to reconnect just yet). */
  1636. if (list_empty(&con->out_queue) && !con->out_keepalive_pending) {
  1637. dout("fault setting STANDBY\n");
  1638. set_bit(STANDBY, &con->state);
  1639. } else {
  1640. /* retry after a delay. */
  1641. if (con->delay == 0)
  1642. con->delay = BASE_DELAY_INTERVAL;
  1643. else if (con->delay < MAX_DELAY_INTERVAL)
  1644. con->delay *= 2;
  1645. dout("fault queueing %p delay %lu\n", con, con->delay);
  1646. con->ops->get(con);
  1647. if (queue_delayed_work(ceph_msgr_wq, &con->work,
  1648. round_jiffies_relative(con->delay)) == 0)
  1649. con->ops->put(con);
  1650. }
  1651. out_unlock:
  1652. mutex_unlock(&con->mutex);
  1653. out:
  1654. /*
  1655. * in case we faulted due to authentication, invalidate our
  1656. * current tickets so that we can get new ones.
  1657. */
  1658. if (con->auth_retry && con->ops->invalidate_authorizer) {
  1659. dout("calling invalidate_authorizer()\n");
  1660. con->ops->invalidate_authorizer(con);
  1661. }
  1662. if (con->ops->fault)
  1663. con->ops->fault(con);
  1664. }
  1665. /*
  1666. * create a new messenger instance
  1667. */
  1668. struct ceph_messenger *ceph_messenger_create(struct ceph_entity_addr *myaddr)
  1669. {
  1670. struct ceph_messenger *msgr;
  1671. msgr = kzalloc(sizeof(*msgr), GFP_KERNEL);
  1672. if (msgr == NULL)
  1673. return ERR_PTR(-ENOMEM);
  1674. spin_lock_init(&msgr->global_seq_lock);
  1675. /* the zero page is needed if a request is "canceled" while the message
  1676. * is being written over the socket */
  1677. msgr->zero_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  1678. if (!msgr->zero_page) {
  1679. kfree(msgr);
  1680. return ERR_PTR(-ENOMEM);
  1681. }
  1682. kmap(msgr->zero_page);
  1683. if (myaddr)
  1684. msgr->inst.addr = *myaddr;
  1685. /* select a random nonce */
  1686. msgr->inst.addr.type = 0;
  1687. get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
  1688. encode_my_addr(msgr);
  1689. dout("messenger_create %p\n", msgr);
  1690. return msgr;
  1691. }
  1692. void ceph_messenger_destroy(struct ceph_messenger *msgr)
  1693. {
  1694. dout("destroy %p\n", msgr);
  1695. kunmap(msgr->zero_page);
  1696. __free_page(msgr->zero_page);
  1697. kfree(msgr);
  1698. dout("destroyed messenger %p\n", msgr);
  1699. }
  1700. /*
  1701. * Queue up an outgoing message on the given connection.
  1702. */
  1703. void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
  1704. {
  1705. if (test_bit(CLOSED, &con->state)) {
  1706. dout("con_send %p closed, dropping %p\n", con, msg);
  1707. ceph_msg_put(msg);
  1708. return;
  1709. }
  1710. /* set src+dst */
  1711. msg->hdr.src.name = con->msgr->inst.name;
  1712. msg->hdr.src.addr = con->msgr->my_enc_addr;
  1713. msg->hdr.orig_src = msg->hdr.src;
  1714. BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
  1715. /* queue */
  1716. mutex_lock(&con->mutex);
  1717. BUG_ON(!list_empty(&msg->list_head));
  1718. list_add_tail(&msg->list_head, &con->out_queue);
  1719. dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
  1720. ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
  1721. ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
  1722. le32_to_cpu(msg->hdr.front_len),
  1723. le32_to_cpu(msg->hdr.middle_len),
  1724. le32_to_cpu(msg->hdr.data_len));
  1725. mutex_unlock(&con->mutex);
  1726. /* if there wasn't anything waiting to send before, queue
  1727. * new work */
  1728. if (test_and_set_bit(WRITE_PENDING, &con->state) == 0)
  1729. queue_con(con);
  1730. }
  1731. /*
  1732. * Revoke a message that was previously queued for send
  1733. */
  1734. void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
  1735. {
  1736. mutex_lock(&con->mutex);
  1737. if (!list_empty(&msg->list_head)) {
  1738. dout("con_revoke %p msg %p\n", con, msg);
  1739. list_del_init(&msg->list_head);
  1740. ceph_msg_put(msg);
  1741. msg->hdr.seq = 0;
  1742. if (con->out_msg == msg) {
  1743. ceph_msg_put(con->out_msg);
  1744. con->out_msg = NULL;
  1745. }
  1746. if (con->out_kvec_is_msg) {
  1747. con->out_skip = con->out_kvec_bytes;
  1748. con->out_kvec_is_msg = false;
  1749. }
  1750. } else {
  1751. dout("con_revoke %p msg %p - not queued (sent?)\n", con, msg);
  1752. }
  1753. mutex_unlock(&con->mutex);
  1754. }
  1755. /*
  1756. * Revoke a message that we may be reading data into
  1757. */
  1758. void ceph_con_revoke_message(struct ceph_connection *con, struct ceph_msg *msg)
  1759. {
  1760. mutex_lock(&con->mutex);
  1761. if (con->in_msg && con->in_msg == msg) {
  1762. unsigned front_len = le32_to_cpu(con->in_hdr.front_len);
  1763. unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len);
  1764. unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
  1765. /* skip rest of message */
  1766. dout("con_revoke_pages %p msg %p revoked\n", con, msg);
  1767. con->in_base_pos = con->in_base_pos -
  1768. sizeof(struct ceph_msg_header) -
  1769. front_len -
  1770. middle_len -
  1771. data_len -
  1772. sizeof(struct ceph_msg_footer);
  1773. ceph_msg_put(con->in_msg);
  1774. con->in_msg = NULL;
  1775. con->in_tag = CEPH_MSGR_TAG_READY;
  1776. } else {
  1777. dout("con_revoke_pages %p msg %p pages %p no-op\n",
  1778. con, con->in_msg, msg);
  1779. }
  1780. mutex_unlock(&con->mutex);
  1781. }
  1782. /*
  1783. * Queue a keepalive byte to ensure the tcp connection is alive.
  1784. */
  1785. void ceph_con_keepalive(struct ceph_connection *con)
  1786. {
  1787. if (test_and_set_bit(KEEPALIVE_PENDING, &con->state) == 0 &&
  1788. test_and_set_bit(WRITE_PENDING, &con->state) == 0)
  1789. queue_con(con);
  1790. }
  1791. /*
  1792. * construct a new message with given type, size
  1793. * the new msg has a ref count of 1.
  1794. */
  1795. struct ceph_msg *ceph_msg_new(int type, int front_len,
  1796. int page_len, int page_off, struct page **pages)
  1797. {
  1798. struct ceph_msg *m;
  1799. m = kmalloc(sizeof(*m), GFP_NOFS);
  1800. if (m == NULL)
  1801. goto out;
  1802. kref_init(&m->kref);
  1803. INIT_LIST_HEAD(&m->list_head);
  1804. m->hdr.type = cpu_to_le16(type);
  1805. m->hdr.front_len = cpu_to_le32(front_len);
  1806. m->hdr.middle_len = 0;
  1807. m->hdr.data_len = cpu_to_le32(page_len);
  1808. m->hdr.data_off = cpu_to_le16(page_off);
  1809. m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
  1810. m->footer.front_crc = 0;
  1811. m->footer.middle_crc = 0;
  1812. m->footer.data_crc = 0;
  1813. m->front_max = front_len;
  1814. m->front_is_vmalloc = false;
  1815. m->more_to_follow = false;
  1816. m->pool = NULL;
  1817. /* front */
  1818. if (front_len) {
  1819. if (front_len > PAGE_CACHE_SIZE) {
  1820. m->front.iov_base = __vmalloc(front_len, GFP_NOFS,
  1821. PAGE_KERNEL);
  1822. m->front_is_vmalloc = true;
  1823. } else {
  1824. m->front.iov_base = kmalloc(front_len, GFP_NOFS);
  1825. }
  1826. if (m->front.iov_base == NULL) {
  1827. pr_err("msg_new can't allocate %d bytes\n",
  1828. front_len);
  1829. goto out2;
  1830. }
  1831. } else {
  1832. m->front.iov_base = NULL;
  1833. }
  1834. m->front.iov_len = front_len;
  1835. /* middle */
  1836. m->middle = NULL;
  1837. /* data */
  1838. m->nr_pages = calc_pages_for(page_off, page_len);
  1839. m->pages = pages;
  1840. m->pagelist = NULL;
  1841. dout("ceph_msg_new %p page %d~%d -> %d\n", m, page_off, page_len,
  1842. m->nr_pages);
  1843. return m;
  1844. out2:
  1845. ceph_msg_put(m);
  1846. out:
  1847. pr_err("msg_new can't create type %d len %d\n", type, front_len);
  1848. return ERR_PTR(-ENOMEM);
  1849. }
  1850. /*
  1851. * Allocate "middle" portion of a message, if it is needed and wasn't
  1852. * allocated by alloc_msg. This allows us to read a small fixed-size
  1853. * per-type header in the front and then gracefully fail (i.e.,
  1854. * propagate the error to the caller based on info in the front) when
  1855. * the middle is too large.
  1856. */
  1857. static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
  1858. {
  1859. int type = le16_to_cpu(msg->hdr.type);
  1860. int middle_len = le32_to_cpu(msg->hdr.middle_len);
  1861. dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
  1862. ceph_msg_type_name(type), middle_len);
  1863. BUG_ON(!middle_len);
  1864. BUG_ON(msg->middle);
  1865. msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
  1866. if (!msg->middle)
  1867. return -ENOMEM;
  1868. return 0;
  1869. }
  1870. /*
  1871. * Generic message allocator, for incoming messages.
  1872. */
  1873. static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
  1874. struct ceph_msg_header *hdr,
  1875. int *skip)
  1876. {
  1877. int type = le16_to_cpu(hdr->type);
  1878. int front_len = le32_to_cpu(hdr->front_len);
  1879. int middle_len = le32_to_cpu(hdr->middle_len);
  1880. struct ceph_msg *msg = NULL;
  1881. int ret;
  1882. if (con->ops->alloc_msg) {
  1883. mutex_unlock(&con->mutex);
  1884. msg = con->ops->alloc_msg(con, hdr, skip);
  1885. mutex_lock(&con->mutex);
  1886. if (IS_ERR(msg))
  1887. return msg;
  1888. if (*skip)
  1889. return NULL;
  1890. }
  1891. if (!msg) {
  1892. *skip = 0;
  1893. msg = ceph_msg_new(type, front_len, 0, 0, NULL);
  1894. if (!msg) {
  1895. pr_err("unable to allocate msg type %d len %d\n",
  1896. type, front_len);
  1897. return ERR_PTR(-ENOMEM);
  1898. }
  1899. }
  1900. memcpy(&msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
  1901. if (middle_len) {
  1902. ret = ceph_alloc_middle(con, msg);
  1903. if (ret < 0) {
  1904. ceph_msg_put(msg);
  1905. return msg;
  1906. }
  1907. }
  1908. return msg;
  1909. }
  1910. /*
  1911. * Free a generically kmalloc'd message.
  1912. */
  1913. void ceph_msg_kfree(struct ceph_msg *m)
  1914. {
  1915. dout("msg_kfree %p\n", m);
  1916. if (m->front_is_vmalloc)
  1917. vfree(m->front.iov_base);
  1918. else
  1919. kfree(m->front.iov_base);
  1920. kfree(m);
  1921. }
  1922. /*
  1923. * Drop a msg ref. Destroy as needed.
  1924. */
  1925. void ceph_msg_last_put(struct kref *kref)
  1926. {
  1927. struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
  1928. dout("ceph_msg_put last one on %p\n", m);
  1929. WARN_ON(!list_empty(&m->list_head));
  1930. /* drop middle, data, if any */
  1931. if (m->middle) {
  1932. ceph_buffer_put(m->middle);
  1933. m->middle = NULL;
  1934. }
  1935. m->nr_pages = 0;
  1936. m->pages = NULL;
  1937. if (m->pagelist) {
  1938. ceph_pagelist_release(m->pagelist);
  1939. kfree(m->pagelist);
  1940. m->pagelist = NULL;
  1941. }
  1942. if (m->pool)
  1943. ceph_msgpool_put(m->pool, m);
  1944. else
  1945. ceph_msg_kfree(m);
  1946. }
  1947. void ceph_msg_dump(struct ceph_msg *msg)
  1948. {
  1949. pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
  1950. msg->front_max, msg->nr_pages);
  1951. print_hex_dump(KERN_DEBUG, "header: ",
  1952. DUMP_PREFIX_OFFSET, 16, 1,
  1953. &msg->hdr, sizeof(msg->hdr), true);
  1954. print_hex_dump(KERN_DEBUG, " front: ",
  1955. DUMP_PREFIX_OFFSET, 16, 1,
  1956. msg->front.iov_base, msg->front.iov_len, true);
  1957. if (msg->middle)
  1958. print_hex_dump(KERN_DEBUG, "middle: ",
  1959. DUMP_PREFIX_OFFSET, 16, 1,
  1960. msg->middle->vec.iov_base,
  1961. msg->middle->vec.iov_len, true);
  1962. print_hex_dump(KERN_DEBUG, "footer: ",
  1963. DUMP_PREFIX_OFFSET, 16, 1,
  1964. &msg->footer, sizeof(msg->footer), true);
  1965. }