messenger.c 56 KB

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