messenger.c 57 KB

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