messenger.c 56 KB

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