messenger.c 61 KB

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