messenger.c 59 KB

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