messenger.c 56 KB

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