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