messenger.c 60 KB

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