messenger.c 57 KB

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