messenger.c 53 KB

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