messenger.c 57 KB

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