messenger.c 52 KB

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