messenger.c 56 KB

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