messenger.c 61 KB

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