messenger.c 61 KB

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