messenger.c 57 KB

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