messenger.c 56 KB

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