messenger.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986
  1. #include <linux/ceph/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. #ifdef CONFIG_BLOCK
  12. #include <linux/bio.h>
  13. #endif /* CONFIG_BLOCK */
  14. #include <linux/dns_resolver.h>
  15. #include <net/tcp.h>
  16. #include <linux/ceph/libceph.h>
  17. #include <linux/ceph/messenger.h>
  18. #include <linux/ceph/decode.h>
  19. #include <linux/ceph/pagelist.h>
  20. #include <linux/export.h>
  21. /*
  22. * Ceph uses the messenger to exchange ceph_msg messages with other
  23. * hosts in the system. The messenger provides ordered and reliable
  24. * delivery. We tolerate TCP disconnects by reconnecting (with
  25. * exponential backoff) in the case of a fault (disconnection, bad
  26. * crc, protocol error). Acks allow sent messages to be discarded by
  27. * the sender.
  28. */
  29. /*
  30. * We track the state of the socket on a given connection using
  31. * values defined below. The transition to a new socket state is
  32. * handled by a function which verifies we aren't coming from an
  33. * unexpected state.
  34. *
  35. * --------
  36. * | NEW* | transient initial state
  37. * --------
  38. * | con_sock_state_init()
  39. * v
  40. * ----------
  41. * | CLOSED | initialized, but no socket (and no
  42. * ---------- TCP connection)
  43. * ^ \
  44. * | \ con_sock_state_connecting()
  45. * | ----------------------
  46. * | \
  47. * + con_sock_state_closed() \
  48. * |+--------------------------- \
  49. * | \ \ \
  50. * | ----------- \ \
  51. * | | CLOSING | socket event; \ \
  52. * | ----------- await close \ \
  53. * | ^ \ |
  54. * | | \ |
  55. * | + con_sock_state_closing() \ |
  56. * | / \ | |
  57. * | / --------------- | |
  58. * | / \ v v
  59. * | / --------------
  60. * | / -----------------| CONNECTING | socket created, TCP
  61. * | | / -------------- connect initiated
  62. * | | | con_sock_state_connected()
  63. * | | v
  64. * -------------
  65. * | CONNECTED | TCP connection established
  66. * -------------
  67. *
  68. * State values for ceph_connection->sock_state; NEW is assumed to be 0.
  69. */
  70. #define CON_SOCK_STATE_NEW 0 /* -> CLOSED */
  71. #define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */
  72. #define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */
  73. #define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */
  74. #define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */
  75. /*
  76. * connection states
  77. */
  78. #define CON_STATE_CLOSED 1 /* -> PREOPEN */
  79. #define CON_STATE_PREOPEN 2 /* -> CONNECTING, CLOSED */
  80. #define CON_STATE_CONNECTING 3 /* -> NEGOTIATING, CLOSED */
  81. #define CON_STATE_NEGOTIATING 4 /* -> OPEN, CLOSED */
  82. #define CON_STATE_OPEN 5 /* -> STANDBY, CLOSED */
  83. #define CON_STATE_STANDBY 6 /* -> PREOPEN, CLOSED */
  84. /*
  85. * ceph_connection flag bits
  86. */
  87. #define CON_FLAG_LOSSYTX 0 /* we can close channel or drop
  88. * messages on errors */
  89. #define CON_FLAG_KEEPALIVE_PENDING 1 /* we need to send a keepalive */
  90. #define CON_FLAG_WRITE_PENDING 2 /* we have data ready to send */
  91. #define CON_FLAG_SOCK_CLOSED 3 /* socket state changed to closed */
  92. #define CON_FLAG_BACKOFF 4 /* need to retry queuing delayed work */
  93. static bool con_flag_valid(unsigned long con_flag)
  94. {
  95. switch (con_flag) {
  96. case CON_FLAG_LOSSYTX:
  97. case CON_FLAG_KEEPALIVE_PENDING:
  98. case CON_FLAG_WRITE_PENDING:
  99. case CON_FLAG_SOCK_CLOSED:
  100. case CON_FLAG_BACKOFF:
  101. return true;
  102. default:
  103. return false;
  104. }
  105. }
  106. static void con_flag_clear(struct ceph_connection *con, unsigned long con_flag)
  107. {
  108. BUG_ON(!con_flag_valid(con_flag));
  109. clear_bit(con_flag, &con->flags);
  110. }
  111. static void con_flag_set(struct ceph_connection *con, unsigned long con_flag)
  112. {
  113. BUG_ON(!con_flag_valid(con_flag));
  114. set_bit(con_flag, &con->flags);
  115. }
  116. static bool con_flag_test(struct ceph_connection *con, unsigned long con_flag)
  117. {
  118. BUG_ON(!con_flag_valid(con_flag));
  119. return test_bit(con_flag, &con->flags);
  120. }
  121. static bool con_flag_test_and_clear(struct ceph_connection *con,
  122. unsigned long con_flag)
  123. {
  124. BUG_ON(!con_flag_valid(con_flag));
  125. return test_and_clear_bit(con_flag, &con->flags);
  126. }
  127. static bool con_flag_test_and_set(struct ceph_connection *con,
  128. unsigned long con_flag)
  129. {
  130. BUG_ON(!con_flag_valid(con_flag));
  131. return test_and_set_bit(con_flag, &con->flags);
  132. }
  133. /* static tag bytes (protocol control messages) */
  134. static char tag_msg = CEPH_MSGR_TAG_MSG;
  135. static char tag_ack = CEPH_MSGR_TAG_ACK;
  136. static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
  137. #ifdef CONFIG_LOCKDEP
  138. static struct lock_class_key socket_class;
  139. #endif
  140. /*
  141. * When skipping (ignoring) a block of input we read it into a "skip
  142. * buffer," which is this many bytes in size.
  143. */
  144. #define SKIP_BUF_SIZE 1024
  145. static void queue_con(struct ceph_connection *con);
  146. static void con_work(struct work_struct *);
  147. static void con_fault(struct ceph_connection *con);
  148. /*
  149. * Nicely render a sockaddr as a string. An array of formatted
  150. * strings is used, to approximate reentrancy.
  151. */
  152. #define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
  153. #define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
  154. #define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
  155. #define MAX_ADDR_STR_LEN 64 /* 54 is enough */
  156. static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
  157. static atomic_t addr_str_seq = ATOMIC_INIT(0);
  158. static struct page *zero_page; /* used in certain error cases */
  159. const char *ceph_pr_addr(const struct sockaddr_storage *ss)
  160. {
  161. int i;
  162. char *s;
  163. struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
  164. struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
  165. i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
  166. s = addr_str[i];
  167. switch (ss->ss_family) {
  168. case AF_INET:
  169. snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
  170. ntohs(in4->sin_port));
  171. break;
  172. case AF_INET6:
  173. snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
  174. ntohs(in6->sin6_port));
  175. break;
  176. default:
  177. snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
  178. ss->ss_family);
  179. }
  180. return s;
  181. }
  182. EXPORT_SYMBOL(ceph_pr_addr);
  183. static void encode_my_addr(struct ceph_messenger *msgr)
  184. {
  185. memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
  186. ceph_encode_addr(&msgr->my_enc_addr);
  187. }
  188. /*
  189. * work queue for all reading and writing to/from the socket.
  190. */
  191. static struct workqueue_struct *ceph_msgr_wq;
  192. static void _ceph_msgr_exit(void)
  193. {
  194. if (ceph_msgr_wq) {
  195. destroy_workqueue(ceph_msgr_wq);
  196. ceph_msgr_wq = NULL;
  197. }
  198. BUG_ON(zero_page == NULL);
  199. kunmap(zero_page);
  200. page_cache_release(zero_page);
  201. zero_page = NULL;
  202. }
  203. int ceph_msgr_init(void)
  204. {
  205. BUG_ON(zero_page != NULL);
  206. zero_page = ZERO_PAGE(0);
  207. page_cache_get(zero_page);
  208. ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0);
  209. if (ceph_msgr_wq)
  210. return 0;
  211. pr_err("msgr_init failed to create workqueue\n");
  212. _ceph_msgr_exit();
  213. return -ENOMEM;
  214. }
  215. EXPORT_SYMBOL(ceph_msgr_init);
  216. void ceph_msgr_exit(void)
  217. {
  218. BUG_ON(ceph_msgr_wq == NULL);
  219. _ceph_msgr_exit();
  220. }
  221. EXPORT_SYMBOL(ceph_msgr_exit);
  222. void ceph_msgr_flush(void)
  223. {
  224. flush_workqueue(ceph_msgr_wq);
  225. }
  226. EXPORT_SYMBOL(ceph_msgr_flush);
  227. /* Connection socket state transition functions */
  228. static void con_sock_state_init(struct ceph_connection *con)
  229. {
  230. int old_state;
  231. old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
  232. if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
  233. printk("%s: unexpected old state %d\n", __func__, old_state);
  234. dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
  235. CON_SOCK_STATE_CLOSED);
  236. }
  237. static void con_sock_state_connecting(struct ceph_connection *con)
  238. {
  239. int old_state;
  240. old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
  241. if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
  242. printk("%s: unexpected old state %d\n", __func__, old_state);
  243. dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
  244. CON_SOCK_STATE_CONNECTING);
  245. }
  246. static void con_sock_state_connected(struct ceph_connection *con)
  247. {
  248. int old_state;
  249. old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
  250. if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
  251. printk("%s: unexpected old state %d\n", __func__, old_state);
  252. dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
  253. CON_SOCK_STATE_CONNECTED);
  254. }
  255. static void con_sock_state_closing(struct ceph_connection *con)
  256. {
  257. int old_state;
  258. old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
  259. if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
  260. old_state != CON_SOCK_STATE_CONNECTED &&
  261. old_state != CON_SOCK_STATE_CLOSING))
  262. printk("%s: unexpected old state %d\n", __func__, old_state);
  263. dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
  264. CON_SOCK_STATE_CLOSING);
  265. }
  266. static void con_sock_state_closed(struct ceph_connection *con)
  267. {
  268. int old_state;
  269. old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
  270. if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
  271. old_state != CON_SOCK_STATE_CLOSING &&
  272. old_state != CON_SOCK_STATE_CONNECTING &&
  273. old_state != CON_SOCK_STATE_CLOSED))
  274. printk("%s: unexpected old state %d\n", __func__, old_state);
  275. dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
  276. CON_SOCK_STATE_CLOSED);
  277. }
  278. /*
  279. * socket callback functions
  280. */
  281. /* data available on socket, or listen socket received a connect */
  282. static void ceph_sock_data_ready(struct sock *sk, int count_unused)
  283. {
  284. struct ceph_connection *con = sk->sk_user_data;
  285. if (atomic_read(&con->msgr->stopping)) {
  286. return;
  287. }
  288. if (sk->sk_state != TCP_CLOSE_WAIT) {
  289. dout("%s on %p state = %lu, queueing work\n", __func__,
  290. con, con->state);
  291. queue_con(con);
  292. }
  293. }
  294. /* socket has buffer space for writing */
  295. static void ceph_sock_write_space(struct sock *sk)
  296. {
  297. struct ceph_connection *con = sk->sk_user_data;
  298. /* only queue to workqueue if there is data we want to write,
  299. * and there is sufficient space in the socket buffer to accept
  300. * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
  301. * doesn't get called again until try_write() fills the socket
  302. * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
  303. * and net/core/stream.c:sk_stream_write_space().
  304. */
  305. if (con_flag_test(con, CON_FLAG_WRITE_PENDING)) {
  306. if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
  307. dout("%s %p queueing write work\n", __func__, con);
  308. clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  309. queue_con(con);
  310. }
  311. } else {
  312. dout("%s %p nothing to write\n", __func__, con);
  313. }
  314. }
  315. /* socket's state has changed */
  316. static void ceph_sock_state_change(struct sock *sk)
  317. {
  318. struct ceph_connection *con = sk->sk_user_data;
  319. dout("%s %p state = %lu sk_state = %u\n", __func__,
  320. con, con->state, sk->sk_state);
  321. switch (sk->sk_state) {
  322. case TCP_CLOSE:
  323. dout("%s TCP_CLOSE\n", __func__);
  324. case TCP_CLOSE_WAIT:
  325. dout("%s TCP_CLOSE_WAIT\n", __func__);
  326. con_sock_state_closing(con);
  327. con_flag_set(con, CON_FLAG_SOCK_CLOSED);
  328. queue_con(con);
  329. break;
  330. case TCP_ESTABLISHED:
  331. dout("%s TCP_ESTABLISHED\n", __func__);
  332. con_sock_state_connected(con);
  333. queue_con(con);
  334. break;
  335. default: /* Everything else is uninteresting */
  336. break;
  337. }
  338. }
  339. /*
  340. * set up socket callbacks
  341. */
  342. static void set_sock_callbacks(struct socket *sock,
  343. struct ceph_connection *con)
  344. {
  345. struct sock *sk = sock->sk;
  346. sk->sk_user_data = con;
  347. sk->sk_data_ready = ceph_sock_data_ready;
  348. sk->sk_write_space = ceph_sock_write_space;
  349. sk->sk_state_change = ceph_sock_state_change;
  350. }
  351. /*
  352. * socket helpers
  353. */
  354. /*
  355. * initiate connection to a remote socket.
  356. */
  357. static int ceph_tcp_connect(struct ceph_connection *con)
  358. {
  359. struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
  360. struct socket *sock;
  361. int ret;
  362. BUG_ON(con->sock);
  363. ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM,
  364. IPPROTO_TCP, &sock);
  365. if (ret)
  366. return ret;
  367. sock->sk->sk_allocation = GFP_NOFS;
  368. #ifdef CONFIG_LOCKDEP
  369. lockdep_set_class(&sock->sk->sk_lock, &socket_class);
  370. #endif
  371. set_sock_callbacks(sock, con);
  372. dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
  373. con_sock_state_connecting(con);
  374. ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
  375. O_NONBLOCK);
  376. if (ret == -EINPROGRESS) {
  377. dout("connect %s EINPROGRESS sk_state = %u\n",
  378. ceph_pr_addr(&con->peer_addr.in_addr),
  379. sock->sk->sk_state);
  380. } else if (ret < 0) {
  381. pr_err("connect %s error %d\n",
  382. ceph_pr_addr(&con->peer_addr.in_addr), ret);
  383. sock_release(sock);
  384. con->error_msg = "connect error";
  385. return ret;
  386. }
  387. con->sock = sock;
  388. return 0;
  389. }
  390. static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
  391. {
  392. struct kvec iov = {buf, len};
  393. struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
  394. int r;
  395. r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
  396. if (r == -EAGAIN)
  397. r = 0;
  398. return r;
  399. }
  400. static int ceph_tcp_recvpage(struct socket *sock, struct page *page,
  401. int page_offset, size_t length)
  402. {
  403. void *kaddr;
  404. int ret;
  405. BUG_ON(page_offset + length > PAGE_SIZE);
  406. kaddr = kmap(page);
  407. BUG_ON(!kaddr);
  408. ret = ceph_tcp_recvmsg(sock, kaddr + page_offset, length);
  409. kunmap(page);
  410. return ret;
  411. }
  412. /*
  413. * write something. @more is true if caller will be sending more data
  414. * shortly.
  415. */
  416. static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
  417. size_t kvlen, size_t len, int more)
  418. {
  419. struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
  420. int r;
  421. if (more)
  422. msg.msg_flags |= MSG_MORE;
  423. else
  424. msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
  425. r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
  426. if (r == -EAGAIN)
  427. r = 0;
  428. return r;
  429. }
  430. static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
  431. int offset, size_t size, bool more)
  432. {
  433. int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
  434. int ret;
  435. ret = kernel_sendpage(sock, page, offset, size, flags);
  436. if (ret == -EAGAIN)
  437. ret = 0;
  438. return ret;
  439. }
  440. /*
  441. * Shutdown/close the socket for the given connection.
  442. */
  443. static int con_close_socket(struct ceph_connection *con)
  444. {
  445. int rc = 0;
  446. dout("con_close_socket on %p sock %p\n", con, con->sock);
  447. if (con->sock) {
  448. rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
  449. sock_release(con->sock);
  450. con->sock = NULL;
  451. }
  452. /*
  453. * Forcibly clear the SOCK_CLOSED flag. It gets set
  454. * independent of the connection mutex, and we could have
  455. * received a socket close event before we had the chance to
  456. * shut the socket down.
  457. */
  458. con_flag_clear(con, CON_FLAG_SOCK_CLOSED);
  459. con_sock_state_closed(con);
  460. return rc;
  461. }
  462. /*
  463. * Reset a connection. Discard all incoming and outgoing messages
  464. * and clear *_seq state.
  465. */
  466. static void ceph_msg_remove(struct ceph_msg *msg)
  467. {
  468. list_del_init(&msg->list_head);
  469. BUG_ON(msg->con == NULL);
  470. msg->con->ops->put(msg->con);
  471. msg->con = NULL;
  472. ceph_msg_put(msg);
  473. }
  474. static void ceph_msg_remove_list(struct list_head *head)
  475. {
  476. while (!list_empty(head)) {
  477. struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
  478. list_head);
  479. ceph_msg_remove(msg);
  480. }
  481. }
  482. static void reset_connection(struct ceph_connection *con)
  483. {
  484. /* reset connection, out_queue, msg_ and connect_seq */
  485. /* discard existing out_queue and msg_seq */
  486. dout("reset_connection %p\n", con);
  487. ceph_msg_remove_list(&con->out_queue);
  488. ceph_msg_remove_list(&con->out_sent);
  489. if (con->in_msg) {
  490. BUG_ON(con->in_msg->con != con);
  491. con->in_msg->con = NULL;
  492. ceph_msg_put(con->in_msg);
  493. con->in_msg = NULL;
  494. con->ops->put(con);
  495. }
  496. con->connect_seq = 0;
  497. con->out_seq = 0;
  498. if (con->out_msg) {
  499. ceph_msg_put(con->out_msg);
  500. con->out_msg = NULL;
  501. }
  502. con->in_seq = 0;
  503. con->in_seq_acked = 0;
  504. }
  505. /*
  506. * mark a peer down. drop any open connections.
  507. */
  508. void ceph_con_close(struct ceph_connection *con)
  509. {
  510. mutex_lock(&con->mutex);
  511. dout("con_close %p peer %s\n", con,
  512. ceph_pr_addr(&con->peer_addr.in_addr));
  513. con->state = CON_STATE_CLOSED;
  514. con_flag_clear(con, CON_FLAG_LOSSYTX); /* so we retry next connect */
  515. con_flag_clear(con, CON_FLAG_KEEPALIVE_PENDING);
  516. con_flag_clear(con, CON_FLAG_WRITE_PENDING);
  517. con_flag_clear(con, CON_FLAG_BACKOFF);
  518. reset_connection(con);
  519. con->peer_global_seq = 0;
  520. cancel_delayed_work(&con->work);
  521. con_close_socket(con);
  522. mutex_unlock(&con->mutex);
  523. }
  524. EXPORT_SYMBOL(ceph_con_close);
  525. /*
  526. * Reopen a closed connection, with a new peer address.
  527. */
  528. void ceph_con_open(struct ceph_connection *con,
  529. __u8 entity_type, __u64 entity_num,
  530. struct ceph_entity_addr *addr)
  531. {
  532. mutex_lock(&con->mutex);
  533. dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
  534. WARN_ON(con->state != CON_STATE_CLOSED);
  535. con->state = CON_STATE_PREOPEN;
  536. con->peer_name.type = (__u8) entity_type;
  537. con->peer_name.num = cpu_to_le64(entity_num);
  538. memcpy(&con->peer_addr, addr, sizeof(*addr));
  539. con->delay = 0; /* reset backoff memory */
  540. mutex_unlock(&con->mutex);
  541. queue_con(con);
  542. }
  543. EXPORT_SYMBOL(ceph_con_open);
  544. /*
  545. * return true if this connection ever successfully opened
  546. */
  547. bool ceph_con_opened(struct ceph_connection *con)
  548. {
  549. return con->connect_seq > 0;
  550. }
  551. /*
  552. * initialize a new connection.
  553. */
  554. void ceph_con_init(struct ceph_connection *con, void *private,
  555. const struct ceph_connection_operations *ops,
  556. struct ceph_messenger *msgr)
  557. {
  558. dout("con_init %p\n", con);
  559. memset(con, 0, sizeof(*con));
  560. con->private = private;
  561. con->ops = ops;
  562. con->msgr = msgr;
  563. con_sock_state_init(con);
  564. mutex_init(&con->mutex);
  565. INIT_LIST_HEAD(&con->out_queue);
  566. INIT_LIST_HEAD(&con->out_sent);
  567. INIT_DELAYED_WORK(&con->work, con_work);
  568. con->state = CON_STATE_CLOSED;
  569. }
  570. EXPORT_SYMBOL(ceph_con_init);
  571. /*
  572. * We maintain a global counter to order connection attempts. Get
  573. * a unique seq greater than @gt.
  574. */
  575. static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
  576. {
  577. u32 ret;
  578. spin_lock(&msgr->global_seq_lock);
  579. if (msgr->global_seq < gt)
  580. msgr->global_seq = gt;
  581. ret = ++msgr->global_seq;
  582. spin_unlock(&msgr->global_seq_lock);
  583. return ret;
  584. }
  585. static void con_out_kvec_reset(struct ceph_connection *con)
  586. {
  587. con->out_kvec_left = 0;
  588. con->out_kvec_bytes = 0;
  589. con->out_kvec_cur = &con->out_kvec[0];
  590. }
  591. static void con_out_kvec_add(struct ceph_connection *con,
  592. size_t size, void *data)
  593. {
  594. int index;
  595. index = con->out_kvec_left;
  596. BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
  597. con->out_kvec[index].iov_len = size;
  598. con->out_kvec[index].iov_base = data;
  599. con->out_kvec_left++;
  600. con->out_kvec_bytes += size;
  601. }
  602. #ifdef CONFIG_BLOCK
  603. static void init_bio_iter(struct bio *bio, struct bio **bio_iter,
  604. unsigned int *bio_seg)
  605. {
  606. if (!bio) {
  607. *bio_iter = NULL;
  608. *bio_seg = 0;
  609. return;
  610. }
  611. *bio_iter = bio;
  612. *bio_seg = (unsigned int) bio->bi_idx;
  613. }
  614. static void iter_bio_next(struct bio **bio_iter, unsigned int *seg)
  615. {
  616. if (*bio_iter == NULL)
  617. return;
  618. BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
  619. (*seg)++;
  620. if (*seg == (*bio_iter)->bi_vcnt)
  621. init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
  622. }
  623. #endif
  624. static void prepare_message_data(struct ceph_msg *msg,
  625. struct ceph_msg_pos *msg_pos)
  626. {
  627. BUG_ON(!msg);
  628. BUG_ON(!msg->hdr.data_len);
  629. /* initialize page iterator */
  630. msg_pos->page = 0;
  631. if (msg->pages)
  632. msg_pos->page_pos = msg->page_alignment;
  633. else
  634. msg_pos->page_pos = 0;
  635. #ifdef CONFIG_BLOCK
  636. if (msg->bio)
  637. init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
  638. #endif
  639. msg_pos->data_pos = 0;
  640. msg_pos->did_page_crc = false;
  641. }
  642. /*
  643. * Prepare footer for currently outgoing message, and finish things
  644. * off. Assumes out_kvec* are already valid.. we just add on to the end.
  645. */
  646. static void prepare_write_message_footer(struct ceph_connection *con)
  647. {
  648. struct ceph_msg *m = con->out_msg;
  649. int v = con->out_kvec_left;
  650. m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
  651. dout("prepare_write_message_footer %p\n", con);
  652. con->out_kvec_is_msg = true;
  653. con->out_kvec[v].iov_base = &m->footer;
  654. con->out_kvec[v].iov_len = sizeof(m->footer);
  655. con->out_kvec_bytes += sizeof(m->footer);
  656. con->out_kvec_left++;
  657. con->out_more = m->more_to_follow;
  658. con->out_msg_done = true;
  659. }
  660. /*
  661. * Prepare headers for the next outgoing message.
  662. */
  663. static void prepare_write_message(struct ceph_connection *con)
  664. {
  665. struct ceph_msg *m;
  666. u32 crc;
  667. con_out_kvec_reset(con);
  668. con->out_kvec_is_msg = true;
  669. con->out_msg_done = false;
  670. /* Sneak an ack in there first? If we can get it into the same
  671. * TCP packet that's a good thing. */
  672. if (con->in_seq > con->in_seq_acked) {
  673. con->in_seq_acked = con->in_seq;
  674. con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
  675. con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
  676. con_out_kvec_add(con, sizeof (con->out_temp_ack),
  677. &con->out_temp_ack);
  678. }
  679. BUG_ON(list_empty(&con->out_queue));
  680. m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
  681. con->out_msg = m;
  682. BUG_ON(m->con != con);
  683. /* put message on sent list */
  684. ceph_msg_get(m);
  685. list_move_tail(&m->list_head, &con->out_sent);
  686. /*
  687. * only assign outgoing seq # if we haven't sent this message
  688. * yet. if it is requeued, resend with it's original seq.
  689. */
  690. if (m->needs_out_seq) {
  691. m->hdr.seq = cpu_to_le64(++con->out_seq);
  692. m->needs_out_seq = false;
  693. }
  694. dout("prepare_write_message %p seq %lld type %d len %d+%d+%d (%zd)\n",
  695. m, con->out_seq, le16_to_cpu(m->hdr.type),
  696. le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
  697. le32_to_cpu(m->hdr.data_len), m->length);
  698. BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
  699. /* tag + hdr + front + middle */
  700. con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
  701. con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
  702. con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
  703. if (m->middle)
  704. con_out_kvec_add(con, m->middle->vec.iov_len,
  705. m->middle->vec.iov_base);
  706. /* fill in crc (except data pages), footer */
  707. crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
  708. con->out_msg->hdr.crc = cpu_to_le32(crc);
  709. con->out_msg->footer.flags = 0;
  710. crc = crc32c(0, m->front.iov_base, m->front.iov_len);
  711. con->out_msg->footer.front_crc = cpu_to_le32(crc);
  712. if (m->middle) {
  713. crc = crc32c(0, m->middle->vec.iov_base,
  714. m->middle->vec.iov_len);
  715. con->out_msg->footer.middle_crc = cpu_to_le32(crc);
  716. } else
  717. con->out_msg->footer.middle_crc = 0;
  718. dout("%s front_crc %u middle_crc %u\n", __func__,
  719. le32_to_cpu(con->out_msg->footer.front_crc),
  720. le32_to_cpu(con->out_msg->footer.middle_crc));
  721. /* is there a data payload? */
  722. con->out_msg->footer.data_crc = 0;
  723. if (m->hdr.data_len) {
  724. prepare_message_data(con->out_msg, &con->out_msg_pos);
  725. con->out_more = 1; /* data + footer will follow */
  726. } else {
  727. /* no, queue up footer too and be done */
  728. prepare_write_message_footer(con);
  729. }
  730. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  731. }
  732. /*
  733. * Prepare an ack.
  734. */
  735. static void prepare_write_ack(struct ceph_connection *con)
  736. {
  737. dout("prepare_write_ack %p %llu -> %llu\n", con,
  738. con->in_seq_acked, con->in_seq);
  739. con->in_seq_acked = con->in_seq;
  740. con_out_kvec_reset(con);
  741. con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
  742. con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
  743. con_out_kvec_add(con, sizeof (con->out_temp_ack),
  744. &con->out_temp_ack);
  745. con->out_more = 1; /* more will follow.. eventually.. */
  746. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  747. }
  748. /*
  749. * Prepare to write keepalive byte.
  750. */
  751. static void prepare_write_keepalive(struct ceph_connection *con)
  752. {
  753. dout("prepare_write_keepalive %p\n", con);
  754. con_out_kvec_reset(con);
  755. con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
  756. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  757. }
  758. /*
  759. * Connection negotiation.
  760. */
  761. static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
  762. int *auth_proto)
  763. {
  764. struct ceph_auth_handshake *auth;
  765. if (!con->ops->get_authorizer) {
  766. con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
  767. con->out_connect.authorizer_len = 0;
  768. return NULL;
  769. }
  770. /* Can't hold the mutex while getting authorizer */
  771. mutex_unlock(&con->mutex);
  772. auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
  773. mutex_lock(&con->mutex);
  774. if (IS_ERR(auth))
  775. return auth;
  776. if (con->state != CON_STATE_NEGOTIATING)
  777. return ERR_PTR(-EAGAIN);
  778. con->auth_reply_buf = auth->authorizer_reply_buf;
  779. con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
  780. return auth;
  781. }
  782. /*
  783. * We connected to a peer and are saying hello.
  784. */
  785. static void prepare_write_banner(struct ceph_connection *con)
  786. {
  787. con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
  788. con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
  789. &con->msgr->my_enc_addr);
  790. con->out_more = 0;
  791. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  792. }
  793. static int prepare_write_connect(struct ceph_connection *con)
  794. {
  795. unsigned int global_seq = get_global_seq(con->msgr, 0);
  796. int proto;
  797. int auth_proto;
  798. struct ceph_auth_handshake *auth;
  799. switch (con->peer_name.type) {
  800. case CEPH_ENTITY_TYPE_MON:
  801. proto = CEPH_MONC_PROTOCOL;
  802. break;
  803. case CEPH_ENTITY_TYPE_OSD:
  804. proto = CEPH_OSDC_PROTOCOL;
  805. break;
  806. case CEPH_ENTITY_TYPE_MDS:
  807. proto = CEPH_MDSC_PROTOCOL;
  808. break;
  809. default:
  810. BUG();
  811. }
  812. dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
  813. con->connect_seq, global_seq, proto);
  814. con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
  815. con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
  816. con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
  817. con->out_connect.global_seq = cpu_to_le32(global_seq);
  818. con->out_connect.protocol_version = cpu_to_le32(proto);
  819. con->out_connect.flags = 0;
  820. auth_proto = CEPH_AUTH_UNKNOWN;
  821. auth = get_connect_authorizer(con, &auth_proto);
  822. if (IS_ERR(auth))
  823. return PTR_ERR(auth);
  824. con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
  825. con->out_connect.authorizer_len = auth ?
  826. cpu_to_le32(auth->authorizer_buf_len) : 0;
  827. con_out_kvec_add(con, sizeof (con->out_connect),
  828. &con->out_connect);
  829. if (auth && auth->authorizer_buf_len)
  830. con_out_kvec_add(con, auth->authorizer_buf_len,
  831. auth->authorizer_buf);
  832. con->out_more = 0;
  833. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  834. return 0;
  835. }
  836. /*
  837. * write as much of pending kvecs to the socket as we can.
  838. * 1 -> done
  839. * 0 -> socket full, but more to do
  840. * <0 -> error
  841. */
  842. static int write_partial_kvec(struct ceph_connection *con)
  843. {
  844. int ret;
  845. dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
  846. while (con->out_kvec_bytes > 0) {
  847. ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
  848. con->out_kvec_left, con->out_kvec_bytes,
  849. con->out_more);
  850. if (ret <= 0)
  851. goto out;
  852. con->out_kvec_bytes -= ret;
  853. if (con->out_kvec_bytes == 0)
  854. break; /* done */
  855. /* account for full iov entries consumed */
  856. while (ret >= con->out_kvec_cur->iov_len) {
  857. BUG_ON(!con->out_kvec_left);
  858. ret -= con->out_kvec_cur->iov_len;
  859. con->out_kvec_cur++;
  860. con->out_kvec_left--;
  861. }
  862. /* and for a partially-consumed entry */
  863. if (ret) {
  864. con->out_kvec_cur->iov_len -= ret;
  865. con->out_kvec_cur->iov_base += ret;
  866. }
  867. }
  868. con->out_kvec_left = 0;
  869. con->out_kvec_is_msg = false;
  870. ret = 1;
  871. out:
  872. dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
  873. con->out_kvec_bytes, con->out_kvec_left, ret);
  874. return ret; /* done! */
  875. }
  876. static void out_msg_pos_next(struct ceph_connection *con, struct page *page,
  877. size_t len, size_t sent, bool in_trail)
  878. {
  879. struct ceph_msg *msg = con->out_msg;
  880. struct ceph_msg_pos *msg_pos = &con->out_msg_pos;
  881. BUG_ON(!msg);
  882. BUG_ON(!sent);
  883. msg_pos->data_pos += sent;
  884. msg_pos->page_pos += sent;
  885. if (sent < len)
  886. return;
  887. BUG_ON(sent != len);
  888. msg_pos->page_pos = 0;
  889. msg_pos->page++;
  890. msg_pos->did_page_crc = false;
  891. if (in_trail)
  892. list_rotate_left(&msg->trail->head);
  893. else if (msg->pagelist)
  894. list_rotate_left(&msg->pagelist->head);
  895. #ifdef CONFIG_BLOCK
  896. else if (msg->bio)
  897. iter_bio_next(&msg->bio_iter, &msg->bio_seg);
  898. #endif
  899. }
  900. static void in_msg_pos_next(struct ceph_connection *con, size_t len,
  901. size_t received)
  902. {
  903. struct ceph_msg *msg = con->in_msg;
  904. struct ceph_msg_pos *msg_pos = &con->in_msg_pos;
  905. BUG_ON(!msg);
  906. BUG_ON(!received);
  907. msg_pos->data_pos += received;
  908. msg_pos->page_pos += received;
  909. if (received < len)
  910. return;
  911. BUG_ON(received != len);
  912. msg_pos->page_pos = 0;
  913. msg_pos->page++;
  914. #ifdef CONFIG_BLOCK
  915. if (msg->bio)
  916. iter_bio_next(&msg->bio_iter, &msg->bio_seg);
  917. #endif /* CONFIG_BLOCK */
  918. }
  919. static u32 ceph_crc32c_page(u32 crc, struct page *page,
  920. unsigned int page_offset,
  921. unsigned int length)
  922. {
  923. char *kaddr;
  924. kaddr = kmap(page);
  925. BUG_ON(kaddr == NULL);
  926. crc = crc32c(crc, kaddr + page_offset, length);
  927. kunmap(page);
  928. return crc;
  929. }
  930. /*
  931. * Write as much message data payload as we can. If we finish, queue
  932. * up the footer.
  933. * 1 -> done, footer is now queued in out_kvec[].
  934. * 0 -> socket full, but more to do
  935. * <0 -> error
  936. */
  937. static int write_partial_message_data(struct ceph_connection *con)
  938. {
  939. struct ceph_msg *msg = con->out_msg;
  940. struct ceph_msg_pos *msg_pos = &con->out_msg_pos;
  941. unsigned int data_len = le32_to_cpu(msg->hdr.data_len);
  942. bool do_datacrc = !con->msgr->nocrc;
  943. int ret;
  944. int total_max_write;
  945. bool in_trail = false;
  946. const size_t trail_len = (msg->trail ? msg->trail->length : 0);
  947. const size_t trail_off = data_len - trail_len;
  948. dout("%s %p msg %p page %d offset %d\n", __func__,
  949. con, msg, msg_pos->page, msg_pos->page_pos);
  950. /*
  951. * Iterate through each page that contains data to be
  952. * written, and send as much as possible for each.
  953. *
  954. * If we are calculating the data crc (the default), we will
  955. * need to map the page. If we have no pages, they have
  956. * been revoked, so use the zero page.
  957. */
  958. while (data_len > msg_pos->data_pos) {
  959. struct page *page = NULL;
  960. size_t page_offset;
  961. size_t length;
  962. int max_write = PAGE_SIZE;
  963. int bio_offset = 0;
  964. in_trail = in_trail || msg_pos->data_pos >= trail_off;
  965. if (!in_trail)
  966. total_max_write = trail_off - msg_pos->data_pos;
  967. if (in_trail) {
  968. total_max_write = data_len - msg_pos->data_pos;
  969. page = list_first_entry(&msg->trail->head,
  970. struct page, lru);
  971. } else if (msg->pages) {
  972. page = msg->pages[msg_pos->page];
  973. } else if (msg->pagelist) {
  974. page = list_first_entry(&msg->pagelist->head,
  975. struct page, lru);
  976. #ifdef CONFIG_BLOCK
  977. } else if (msg->bio) {
  978. struct bio_vec *bv;
  979. bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
  980. page = bv->bv_page;
  981. bio_offset = bv->bv_offset;
  982. max_write = bv->bv_len;
  983. #endif
  984. } else {
  985. page = zero_page;
  986. }
  987. length = min_t(int, max_write - msg_pos->page_pos,
  988. total_max_write);
  989. page_offset = msg_pos->page_pos + bio_offset;
  990. if (do_datacrc && !msg_pos->did_page_crc) {
  991. u32 crc = le32_to_cpu(msg->footer.data_crc);
  992. crc = ceph_crc32c_page(crc, page, page_offset, length);
  993. msg->footer.data_crc = cpu_to_le32(crc);
  994. msg_pos->did_page_crc = true;
  995. }
  996. ret = ceph_tcp_sendpage(con->sock, page, page_offset,
  997. length, true);
  998. if (ret <= 0)
  999. goto out;
  1000. out_msg_pos_next(con, page, length, (size_t) ret, in_trail);
  1001. }
  1002. dout("%s %p msg %p done\n", __func__, con, msg);
  1003. /* prepare and queue up footer, too */
  1004. if (!do_datacrc)
  1005. msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
  1006. con_out_kvec_reset(con);
  1007. prepare_write_message_footer(con);
  1008. ret = 1;
  1009. out:
  1010. return ret;
  1011. }
  1012. /*
  1013. * write some zeros
  1014. */
  1015. static int write_partial_skip(struct ceph_connection *con)
  1016. {
  1017. int ret;
  1018. while (con->out_skip > 0) {
  1019. size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
  1020. ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, true);
  1021. if (ret <= 0)
  1022. goto out;
  1023. con->out_skip -= ret;
  1024. }
  1025. ret = 1;
  1026. out:
  1027. return ret;
  1028. }
  1029. /*
  1030. * Prepare to read connection handshake, or an ack.
  1031. */
  1032. static void prepare_read_banner(struct ceph_connection *con)
  1033. {
  1034. dout("prepare_read_banner %p\n", con);
  1035. con->in_base_pos = 0;
  1036. }
  1037. static void prepare_read_connect(struct ceph_connection *con)
  1038. {
  1039. dout("prepare_read_connect %p\n", con);
  1040. con->in_base_pos = 0;
  1041. }
  1042. static void prepare_read_ack(struct ceph_connection *con)
  1043. {
  1044. dout("prepare_read_ack %p\n", con);
  1045. con->in_base_pos = 0;
  1046. }
  1047. static void prepare_read_tag(struct ceph_connection *con)
  1048. {
  1049. dout("prepare_read_tag %p\n", con);
  1050. con->in_base_pos = 0;
  1051. con->in_tag = CEPH_MSGR_TAG_READY;
  1052. }
  1053. /*
  1054. * Prepare to read a message.
  1055. */
  1056. static int prepare_read_message(struct ceph_connection *con)
  1057. {
  1058. dout("prepare_read_message %p\n", con);
  1059. BUG_ON(con->in_msg != NULL);
  1060. con->in_base_pos = 0;
  1061. con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
  1062. return 0;
  1063. }
  1064. static int read_partial(struct ceph_connection *con,
  1065. int end, int size, void *object)
  1066. {
  1067. while (con->in_base_pos < end) {
  1068. int left = end - con->in_base_pos;
  1069. int have = size - left;
  1070. int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
  1071. if (ret <= 0)
  1072. return ret;
  1073. con->in_base_pos += ret;
  1074. }
  1075. return 1;
  1076. }
  1077. /*
  1078. * Read all or part of the connect-side handshake on a new connection
  1079. */
  1080. static int read_partial_banner(struct ceph_connection *con)
  1081. {
  1082. int size;
  1083. int end;
  1084. int ret;
  1085. dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
  1086. /* peer's banner */
  1087. size = strlen(CEPH_BANNER);
  1088. end = size;
  1089. ret = read_partial(con, end, size, con->in_banner);
  1090. if (ret <= 0)
  1091. goto out;
  1092. size = sizeof (con->actual_peer_addr);
  1093. end += size;
  1094. ret = read_partial(con, end, size, &con->actual_peer_addr);
  1095. if (ret <= 0)
  1096. goto out;
  1097. size = sizeof (con->peer_addr_for_me);
  1098. end += size;
  1099. ret = read_partial(con, end, size, &con->peer_addr_for_me);
  1100. if (ret <= 0)
  1101. goto out;
  1102. out:
  1103. return ret;
  1104. }
  1105. static int read_partial_connect(struct ceph_connection *con)
  1106. {
  1107. int size;
  1108. int end;
  1109. int ret;
  1110. dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
  1111. size = sizeof (con->in_reply);
  1112. end = size;
  1113. ret = read_partial(con, end, size, &con->in_reply);
  1114. if (ret <= 0)
  1115. goto out;
  1116. size = le32_to_cpu(con->in_reply.authorizer_len);
  1117. end += size;
  1118. ret = read_partial(con, end, size, con->auth_reply_buf);
  1119. if (ret <= 0)
  1120. goto out;
  1121. dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
  1122. con, (int)con->in_reply.tag,
  1123. le32_to_cpu(con->in_reply.connect_seq),
  1124. le32_to_cpu(con->in_reply.global_seq));
  1125. out:
  1126. return ret;
  1127. }
  1128. /*
  1129. * Verify the hello banner looks okay.
  1130. */
  1131. static int verify_hello(struct ceph_connection *con)
  1132. {
  1133. if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
  1134. pr_err("connect to %s got bad banner\n",
  1135. ceph_pr_addr(&con->peer_addr.in_addr));
  1136. con->error_msg = "protocol error, bad banner";
  1137. return -1;
  1138. }
  1139. return 0;
  1140. }
  1141. static bool addr_is_blank(struct sockaddr_storage *ss)
  1142. {
  1143. switch (ss->ss_family) {
  1144. case AF_INET:
  1145. return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
  1146. case AF_INET6:
  1147. return
  1148. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
  1149. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
  1150. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
  1151. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
  1152. }
  1153. return false;
  1154. }
  1155. static int addr_port(struct sockaddr_storage *ss)
  1156. {
  1157. switch (ss->ss_family) {
  1158. case AF_INET:
  1159. return ntohs(((struct sockaddr_in *)ss)->sin_port);
  1160. case AF_INET6:
  1161. return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
  1162. }
  1163. return 0;
  1164. }
  1165. static void addr_set_port(struct sockaddr_storage *ss, int p)
  1166. {
  1167. switch (ss->ss_family) {
  1168. case AF_INET:
  1169. ((struct sockaddr_in *)ss)->sin_port = htons(p);
  1170. break;
  1171. case AF_INET6:
  1172. ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
  1173. break;
  1174. }
  1175. }
  1176. /*
  1177. * Unlike other *_pton function semantics, zero indicates success.
  1178. */
  1179. static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
  1180. char delim, const char **ipend)
  1181. {
  1182. struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
  1183. struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
  1184. memset(ss, 0, sizeof(*ss));
  1185. if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
  1186. ss->ss_family = AF_INET;
  1187. return 0;
  1188. }
  1189. if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
  1190. ss->ss_family = AF_INET6;
  1191. return 0;
  1192. }
  1193. return -EINVAL;
  1194. }
  1195. /*
  1196. * Extract hostname string and resolve using kernel DNS facility.
  1197. */
  1198. #ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
  1199. static int ceph_dns_resolve_name(const char *name, size_t namelen,
  1200. struct sockaddr_storage *ss, char delim, const char **ipend)
  1201. {
  1202. const char *end, *delim_p;
  1203. char *colon_p, *ip_addr = NULL;
  1204. int ip_len, ret;
  1205. /*
  1206. * The end of the hostname occurs immediately preceding the delimiter or
  1207. * the port marker (':') where the delimiter takes precedence.
  1208. */
  1209. delim_p = memchr(name, delim, namelen);
  1210. colon_p = memchr(name, ':', namelen);
  1211. if (delim_p && colon_p)
  1212. end = delim_p < colon_p ? delim_p : colon_p;
  1213. else if (!delim_p && colon_p)
  1214. end = colon_p;
  1215. else {
  1216. end = delim_p;
  1217. if (!end) /* case: hostname:/ */
  1218. end = name + namelen;
  1219. }
  1220. if (end <= name)
  1221. return -EINVAL;
  1222. /* do dns_resolve upcall */
  1223. ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
  1224. if (ip_len > 0)
  1225. ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
  1226. else
  1227. ret = -ESRCH;
  1228. kfree(ip_addr);
  1229. *ipend = end;
  1230. pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
  1231. ret, ret ? "failed" : ceph_pr_addr(ss));
  1232. return ret;
  1233. }
  1234. #else
  1235. static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
  1236. struct sockaddr_storage *ss, char delim, const char **ipend)
  1237. {
  1238. return -EINVAL;
  1239. }
  1240. #endif
  1241. /*
  1242. * Parse a server name (IP or hostname). If a valid IP address is not found
  1243. * then try to extract a hostname to resolve using userspace DNS upcall.
  1244. */
  1245. static int ceph_parse_server_name(const char *name, size_t namelen,
  1246. struct sockaddr_storage *ss, char delim, const char **ipend)
  1247. {
  1248. int ret;
  1249. ret = ceph_pton(name, namelen, ss, delim, ipend);
  1250. if (ret)
  1251. ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
  1252. return ret;
  1253. }
  1254. /*
  1255. * Parse an ip[:port] list into an addr array. Use the default
  1256. * monitor port if a port isn't specified.
  1257. */
  1258. int ceph_parse_ips(const char *c, const char *end,
  1259. struct ceph_entity_addr *addr,
  1260. int max_count, int *count)
  1261. {
  1262. int i, ret = -EINVAL;
  1263. const char *p = c;
  1264. dout("parse_ips on '%.*s'\n", (int)(end-c), c);
  1265. for (i = 0; i < max_count; i++) {
  1266. const char *ipend;
  1267. struct sockaddr_storage *ss = &addr[i].in_addr;
  1268. int port;
  1269. char delim = ',';
  1270. if (*p == '[') {
  1271. delim = ']';
  1272. p++;
  1273. }
  1274. ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
  1275. if (ret)
  1276. goto bad;
  1277. ret = -EINVAL;
  1278. p = ipend;
  1279. if (delim == ']') {
  1280. if (*p != ']') {
  1281. dout("missing matching ']'\n");
  1282. goto bad;
  1283. }
  1284. p++;
  1285. }
  1286. /* port? */
  1287. if (p < end && *p == ':') {
  1288. port = 0;
  1289. p++;
  1290. while (p < end && *p >= '0' && *p <= '9') {
  1291. port = (port * 10) + (*p - '0');
  1292. p++;
  1293. }
  1294. if (port > 65535 || port == 0)
  1295. goto bad;
  1296. } else {
  1297. port = CEPH_MON_PORT;
  1298. }
  1299. addr_set_port(ss, port);
  1300. dout("parse_ips got %s\n", ceph_pr_addr(ss));
  1301. if (p == end)
  1302. break;
  1303. if (*p != ',')
  1304. goto bad;
  1305. p++;
  1306. }
  1307. if (p != end)
  1308. goto bad;
  1309. if (count)
  1310. *count = i + 1;
  1311. return 0;
  1312. bad:
  1313. pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
  1314. return ret;
  1315. }
  1316. EXPORT_SYMBOL(ceph_parse_ips);
  1317. static int process_banner(struct ceph_connection *con)
  1318. {
  1319. dout("process_banner on %p\n", con);
  1320. if (verify_hello(con) < 0)
  1321. return -1;
  1322. ceph_decode_addr(&con->actual_peer_addr);
  1323. ceph_decode_addr(&con->peer_addr_for_me);
  1324. /*
  1325. * Make sure the other end is who we wanted. note that the other
  1326. * end may not yet know their ip address, so if it's 0.0.0.0, give
  1327. * them the benefit of the doubt.
  1328. */
  1329. if (memcmp(&con->peer_addr, &con->actual_peer_addr,
  1330. sizeof(con->peer_addr)) != 0 &&
  1331. !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
  1332. con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
  1333. pr_warning("wrong peer, want %s/%d, got %s/%d\n",
  1334. ceph_pr_addr(&con->peer_addr.in_addr),
  1335. (int)le32_to_cpu(con->peer_addr.nonce),
  1336. ceph_pr_addr(&con->actual_peer_addr.in_addr),
  1337. (int)le32_to_cpu(con->actual_peer_addr.nonce));
  1338. con->error_msg = "wrong peer at address";
  1339. return -1;
  1340. }
  1341. /*
  1342. * did we learn our address?
  1343. */
  1344. if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
  1345. int port = addr_port(&con->msgr->inst.addr.in_addr);
  1346. memcpy(&con->msgr->inst.addr.in_addr,
  1347. &con->peer_addr_for_me.in_addr,
  1348. sizeof(con->peer_addr_for_me.in_addr));
  1349. addr_set_port(&con->msgr->inst.addr.in_addr, port);
  1350. encode_my_addr(con->msgr);
  1351. dout("process_banner learned my addr is %s\n",
  1352. ceph_pr_addr(&con->msgr->inst.addr.in_addr));
  1353. }
  1354. return 0;
  1355. }
  1356. static int process_connect(struct ceph_connection *con)
  1357. {
  1358. u64 sup_feat = con->msgr->supported_features;
  1359. u64 req_feat = con->msgr->required_features;
  1360. u64 server_feat = le64_to_cpu(con->in_reply.features);
  1361. int ret;
  1362. dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
  1363. switch (con->in_reply.tag) {
  1364. case CEPH_MSGR_TAG_FEATURES:
  1365. pr_err("%s%lld %s feature set mismatch,"
  1366. " my %llx < server's %llx, missing %llx\n",
  1367. ENTITY_NAME(con->peer_name),
  1368. ceph_pr_addr(&con->peer_addr.in_addr),
  1369. sup_feat, server_feat, server_feat & ~sup_feat);
  1370. con->error_msg = "missing required protocol features";
  1371. reset_connection(con);
  1372. return -1;
  1373. case CEPH_MSGR_TAG_BADPROTOVER:
  1374. pr_err("%s%lld %s protocol version mismatch,"
  1375. " my %d != server's %d\n",
  1376. ENTITY_NAME(con->peer_name),
  1377. ceph_pr_addr(&con->peer_addr.in_addr),
  1378. le32_to_cpu(con->out_connect.protocol_version),
  1379. le32_to_cpu(con->in_reply.protocol_version));
  1380. con->error_msg = "protocol version mismatch";
  1381. reset_connection(con);
  1382. return -1;
  1383. case CEPH_MSGR_TAG_BADAUTHORIZER:
  1384. con->auth_retry++;
  1385. dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
  1386. con->auth_retry);
  1387. if (con->auth_retry == 2) {
  1388. con->error_msg = "connect authorization failure";
  1389. return -1;
  1390. }
  1391. con->auth_retry = 1;
  1392. con_out_kvec_reset(con);
  1393. ret = prepare_write_connect(con);
  1394. if (ret < 0)
  1395. return ret;
  1396. prepare_read_connect(con);
  1397. break;
  1398. case CEPH_MSGR_TAG_RESETSESSION:
  1399. /*
  1400. * If we connected with a large connect_seq but the peer
  1401. * has no record of a session with us (no connection, or
  1402. * connect_seq == 0), they will send RESETSESION to indicate
  1403. * that they must have reset their session, and may have
  1404. * dropped messages.
  1405. */
  1406. dout("process_connect got RESET peer seq %u\n",
  1407. le32_to_cpu(con->in_reply.connect_seq));
  1408. pr_err("%s%lld %s connection reset\n",
  1409. ENTITY_NAME(con->peer_name),
  1410. ceph_pr_addr(&con->peer_addr.in_addr));
  1411. reset_connection(con);
  1412. con_out_kvec_reset(con);
  1413. ret = prepare_write_connect(con);
  1414. if (ret < 0)
  1415. return ret;
  1416. prepare_read_connect(con);
  1417. /* Tell ceph about it. */
  1418. mutex_unlock(&con->mutex);
  1419. pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
  1420. if (con->ops->peer_reset)
  1421. con->ops->peer_reset(con);
  1422. mutex_lock(&con->mutex);
  1423. if (con->state != CON_STATE_NEGOTIATING)
  1424. return -EAGAIN;
  1425. break;
  1426. case CEPH_MSGR_TAG_RETRY_SESSION:
  1427. /*
  1428. * If we sent a smaller connect_seq than the peer has, try
  1429. * again with a larger value.
  1430. */
  1431. dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
  1432. le32_to_cpu(con->out_connect.connect_seq),
  1433. le32_to_cpu(con->in_reply.connect_seq));
  1434. con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
  1435. con_out_kvec_reset(con);
  1436. ret = prepare_write_connect(con);
  1437. if (ret < 0)
  1438. return ret;
  1439. prepare_read_connect(con);
  1440. break;
  1441. case CEPH_MSGR_TAG_RETRY_GLOBAL:
  1442. /*
  1443. * If we sent a smaller global_seq than the peer has, try
  1444. * again with a larger value.
  1445. */
  1446. dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
  1447. con->peer_global_seq,
  1448. le32_to_cpu(con->in_reply.global_seq));
  1449. get_global_seq(con->msgr,
  1450. le32_to_cpu(con->in_reply.global_seq));
  1451. con_out_kvec_reset(con);
  1452. ret = prepare_write_connect(con);
  1453. if (ret < 0)
  1454. return ret;
  1455. prepare_read_connect(con);
  1456. break;
  1457. case CEPH_MSGR_TAG_READY:
  1458. if (req_feat & ~server_feat) {
  1459. pr_err("%s%lld %s protocol feature mismatch,"
  1460. " my required %llx > server's %llx, need %llx\n",
  1461. ENTITY_NAME(con->peer_name),
  1462. ceph_pr_addr(&con->peer_addr.in_addr),
  1463. req_feat, server_feat, req_feat & ~server_feat);
  1464. con->error_msg = "missing required protocol features";
  1465. reset_connection(con);
  1466. return -1;
  1467. }
  1468. WARN_ON(con->state != CON_STATE_NEGOTIATING);
  1469. con->state = CON_STATE_OPEN;
  1470. con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
  1471. con->connect_seq++;
  1472. con->peer_features = server_feat;
  1473. dout("process_connect got READY gseq %d cseq %d (%d)\n",
  1474. con->peer_global_seq,
  1475. le32_to_cpu(con->in_reply.connect_seq),
  1476. con->connect_seq);
  1477. WARN_ON(con->connect_seq !=
  1478. le32_to_cpu(con->in_reply.connect_seq));
  1479. if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
  1480. con_flag_set(con, CON_FLAG_LOSSYTX);
  1481. con->delay = 0; /* reset backoff memory */
  1482. prepare_read_tag(con);
  1483. break;
  1484. case CEPH_MSGR_TAG_WAIT:
  1485. /*
  1486. * If there is a connection race (we are opening
  1487. * connections to each other), one of us may just have
  1488. * to WAIT. This shouldn't happen if we are the
  1489. * client.
  1490. */
  1491. pr_err("process_connect got WAIT as client\n");
  1492. con->error_msg = "protocol error, got WAIT as client";
  1493. return -1;
  1494. default:
  1495. pr_err("connect protocol error, will retry\n");
  1496. con->error_msg = "protocol error, garbage tag during connect";
  1497. return -1;
  1498. }
  1499. return 0;
  1500. }
  1501. /*
  1502. * read (part of) an ack
  1503. */
  1504. static int read_partial_ack(struct ceph_connection *con)
  1505. {
  1506. int size = sizeof (con->in_temp_ack);
  1507. int end = size;
  1508. return read_partial(con, end, size, &con->in_temp_ack);
  1509. }
  1510. /*
  1511. * We can finally discard anything that's been acked.
  1512. */
  1513. static void process_ack(struct ceph_connection *con)
  1514. {
  1515. struct ceph_msg *m;
  1516. u64 ack = le64_to_cpu(con->in_temp_ack);
  1517. u64 seq;
  1518. while (!list_empty(&con->out_sent)) {
  1519. m = list_first_entry(&con->out_sent, struct ceph_msg,
  1520. list_head);
  1521. seq = le64_to_cpu(m->hdr.seq);
  1522. if (seq > ack)
  1523. break;
  1524. dout("got ack for seq %llu type %d at %p\n", seq,
  1525. le16_to_cpu(m->hdr.type), m);
  1526. m->ack_stamp = jiffies;
  1527. ceph_msg_remove(m);
  1528. }
  1529. prepare_read_tag(con);
  1530. }
  1531. static int read_partial_message_section(struct ceph_connection *con,
  1532. struct kvec *section,
  1533. unsigned int sec_len, u32 *crc)
  1534. {
  1535. int ret, left;
  1536. BUG_ON(!section);
  1537. while (section->iov_len < sec_len) {
  1538. BUG_ON(section->iov_base == NULL);
  1539. left = sec_len - section->iov_len;
  1540. ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
  1541. section->iov_len, left);
  1542. if (ret <= 0)
  1543. return ret;
  1544. section->iov_len += ret;
  1545. }
  1546. if (section->iov_len == sec_len)
  1547. *crc = crc32c(0, section->iov_base, section->iov_len);
  1548. return 1;
  1549. }
  1550. static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip);
  1551. static int read_partial_message_pages(struct ceph_connection *con,
  1552. struct page **pages,
  1553. unsigned int data_len, bool do_datacrc)
  1554. {
  1555. struct ceph_msg_pos *msg_pos = &con->in_msg_pos;
  1556. struct page *page;
  1557. size_t page_offset;
  1558. size_t length;
  1559. unsigned int left;
  1560. int ret;
  1561. /* (page) data */
  1562. BUG_ON(pages == NULL);
  1563. page = pages[msg_pos->page];
  1564. page_offset = msg_pos->page_pos;
  1565. BUG_ON(msg_pos->data_pos >= data_len);
  1566. left = data_len - msg_pos->data_pos;
  1567. BUG_ON(page_offset >= PAGE_SIZE);
  1568. length = min_t(unsigned int, PAGE_SIZE - page_offset, left);
  1569. ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
  1570. if (ret <= 0)
  1571. return ret;
  1572. if (do_datacrc)
  1573. con->in_data_crc = ceph_crc32c_page(con->in_data_crc, page,
  1574. page_offset, ret);
  1575. in_msg_pos_next(con, length, ret);
  1576. return ret;
  1577. }
  1578. #ifdef CONFIG_BLOCK
  1579. static int read_partial_message_bio(struct ceph_connection *con,
  1580. unsigned int data_len, bool do_datacrc)
  1581. {
  1582. struct ceph_msg *msg = con->in_msg;
  1583. struct ceph_msg_pos *msg_pos = &con->in_msg_pos;
  1584. struct bio_vec *bv;
  1585. struct page *page;
  1586. size_t page_offset;
  1587. size_t length;
  1588. unsigned int left;
  1589. int ret;
  1590. BUG_ON(!msg);
  1591. BUG_ON(!msg->bio_iter);
  1592. bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
  1593. page = bv->bv_page;
  1594. page_offset = bv->bv_offset + msg_pos->page_pos;
  1595. BUG_ON(msg_pos->data_pos >= data_len);
  1596. left = data_len - msg_pos->data_pos;
  1597. BUG_ON(msg_pos->page_pos >= bv->bv_len);
  1598. length = min_t(unsigned int, bv->bv_len - msg_pos->page_pos, left);
  1599. ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
  1600. if (ret <= 0)
  1601. return ret;
  1602. if (do_datacrc)
  1603. con->in_data_crc = ceph_crc32c_page(con->in_data_crc, page,
  1604. page_offset, ret);
  1605. in_msg_pos_next(con, length, ret);
  1606. return ret;
  1607. }
  1608. #endif
  1609. static int read_partial_msg_data(struct ceph_connection *con)
  1610. {
  1611. struct ceph_msg *msg = con->in_msg;
  1612. struct ceph_msg_pos *msg_pos = &con->in_msg_pos;
  1613. const bool do_datacrc = !con->msgr->nocrc;
  1614. unsigned int data_len;
  1615. int ret;
  1616. BUG_ON(!msg);
  1617. data_len = le32_to_cpu(con->in_hdr.data_len);
  1618. while (msg_pos->data_pos < data_len) {
  1619. if (msg->pages) {
  1620. ret = read_partial_message_pages(con, msg->pages,
  1621. data_len, do_datacrc);
  1622. if (ret <= 0)
  1623. return ret;
  1624. #ifdef CONFIG_BLOCK
  1625. } else if (msg->bio) {
  1626. ret = read_partial_message_bio(con,
  1627. data_len, do_datacrc);
  1628. if (ret <= 0)
  1629. return ret;
  1630. #endif
  1631. } else {
  1632. BUG_ON(1);
  1633. }
  1634. }
  1635. return 1; /* must return > 0 to indicate success */
  1636. }
  1637. /*
  1638. * read (part of) a message.
  1639. */
  1640. static int read_partial_message(struct ceph_connection *con)
  1641. {
  1642. struct ceph_msg *m = con->in_msg;
  1643. int size;
  1644. int end;
  1645. int ret;
  1646. unsigned int front_len, middle_len, data_len;
  1647. bool do_datacrc = !con->msgr->nocrc;
  1648. u64 seq;
  1649. u32 crc;
  1650. dout("read_partial_message con %p msg %p\n", con, m);
  1651. /* header */
  1652. size = sizeof (con->in_hdr);
  1653. end = size;
  1654. ret = read_partial(con, end, size, &con->in_hdr);
  1655. if (ret <= 0)
  1656. return ret;
  1657. crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
  1658. if (cpu_to_le32(crc) != con->in_hdr.crc) {
  1659. pr_err("read_partial_message bad hdr "
  1660. " crc %u != expected %u\n",
  1661. crc, con->in_hdr.crc);
  1662. return -EBADMSG;
  1663. }
  1664. front_len = le32_to_cpu(con->in_hdr.front_len);
  1665. if (front_len > CEPH_MSG_MAX_FRONT_LEN)
  1666. return -EIO;
  1667. middle_len = le32_to_cpu(con->in_hdr.middle_len);
  1668. if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
  1669. return -EIO;
  1670. data_len = le32_to_cpu(con->in_hdr.data_len);
  1671. if (data_len > CEPH_MSG_MAX_DATA_LEN)
  1672. return -EIO;
  1673. /* verify seq# */
  1674. seq = le64_to_cpu(con->in_hdr.seq);
  1675. if ((s64)seq - (s64)con->in_seq < 1) {
  1676. pr_info("skipping %s%lld %s seq %lld expected %lld\n",
  1677. ENTITY_NAME(con->peer_name),
  1678. ceph_pr_addr(&con->peer_addr.in_addr),
  1679. seq, con->in_seq + 1);
  1680. con->in_base_pos = -front_len - middle_len - data_len -
  1681. sizeof(m->footer);
  1682. con->in_tag = CEPH_MSGR_TAG_READY;
  1683. return 0;
  1684. } else if ((s64)seq - (s64)con->in_seq > 1) {
  1685. pr_err("read_partial_message bad seq %lld expected %lld\n",
  1686. seq, con->in_seq + 1);
  1687. con->error_msg = "bad message sequence # for incoming message";
  1688. return -EBADMSG;
  1689. }
  1690. /* allocate message? */
  1691. if (!con->in_msg) {
  1692. int skip = 0;
  1693. dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
  1694. front_len, data_len);
  1695. ret = ceph_con_in_msg_alloc(con, &skip);
  1696. if (ret < 0)
  1697. return ret;
  1698. if (skip) {
  1699. /* skip this message */
  1700. dout("alloc_msg said skip message\n");
  1701. BUG_ON(con->in_msg);
  1702. con->in_base_pos = -front_len - middle_len - data_len -
  1703. sizeof(m->footer);
  1704. con->in_tag = CEPH_MSGR_TAG_READY;
  1705. con->in_seq++;
  1706. return 0;
  1707. }
  1708. BUG_ON(!con->in_msg);
  1709. BUG_ON(con->in_msg->con != con);
  1710. m = con->in_msg;
  1711. m->front.iov_len = 0; /* haven't read it yet */
  1712. if (m->middle)
  1713. m->middle->vec.iov_len = 0;
  1714. /* prepare for data payload, if any */
  1715. if (data_len)
  1716. prepare_message_data(con->in_msg, &con->in_msg_pos);
  1717. }
  1718. /* front */
  1719. ret = read_partial_message_section(con, &m->front, front_len,
  1720. &con->in_front_crc);
  1721. if (ret <= 0)
  1722. return ret;
  1723. /* middle */
  1724. if (m->middle) {
  1725. ret = read_partial_message_section(con, &m->middle->vec,
  1726. middle_len,
  1727. &con->in_middle_crc);
  1728. if (ret <= 0)
  1729. return ret;
  1730. }
  1731. /* (page) data */
  1732. if (data_len) {
  1733. ret = read_partial_msg_data(con);
  1734. if (ret <= 0)
  1735. return ret;
  1736. }
  1737. /* footer */
  1738. size = sizeof (m->footer);
  1739. end += size;
  1740. ret = read_partial(con, end, size, &m->footer);
  1741. if (ret <= 0)
  1742. return ret;
  1743. dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
  1744. m, front_len, m->footer.front_crc, middle_len,
  1745. m->footer.middle_crc, data_len, m->footer.data_crc);
  1746. /* crc ok? */
  1747. if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
  1748. pr_err("read_partial_message %p front crc %u != exp. %u\n",
  1749. m, con->in_front_crc, m->footer.front_crc);
  1750. return -EBADMSG;
  1751. }
  1752. if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
  1753. pr_err("read_partial_message %p middle crc %u != exp %u\n",
  1754. m, con->in_middle_crc, m->footer.middle_crc);
  1755. return -EBADMSG;
  1756. }
  1757. if (do_datacrc &&
  1758. (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
  1759. con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
  1760. pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
  1761. con->in_data_crc, le32_to_cpu(m->footer.data_crc));
  1762. return -EBADMSG;
  1763. }
  1764. return 1; /* done! */
  1765. }
  1766. /*
  1767. * Process message. This happens in the worker thread. The callback should
  1768. * be careful not to do anything that waits on other incoming messages or it
  1769. * may deadlock.
  1770. */
  1771. static void process_message(struct ceph_connection *con)
  1772. {
  1773. struct ceph_msg *msg;
  1774. BUG_ON(con->in_msg->con != con);
  1775. con->in_msg->con = NULL;
  1776. msg = con->in_msg;
  1777. con->in_msg = NULL;
  1778. con->ops->put(con);
  1779. /* if first message, set peer_name */
  1780. if (con->peer_name.type == 0)
  1781. con->peer_name = msg->hdr.src;
  1782. con->in_seq++;
  1783. mutex_unlock(&con->mutex);
  1784. dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
  1785. msg, le64_to_cpu(msg->hdr.seq),
  1786. ENTITY_NAME(msg->hdr.src),
  1787. le16_to_cpu(msg->hdr.type),
  1788. ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
  1789. le32_to_cpu(msg->hdr.front_len),
  1790. le32_to_cpu(msg->hdr.data_len),
  1791. con->in_front_crc, con->in_middle_crc, con->in_data_crc);
  1792. con->ops->dispatch(con, msg);
  1793. mutex_lock(&con->mutex);
  1794. }
  1795. /*
  1796. * Write something to the socket. Called in a worker thread when the
  1797. * socket appears to be writeable and we have something ready to send.
  1798. */
  1799. static int try_write(struct ceph_connection *con)
  1800. {
  1801. int ret = 1;
  1802. dout("try_write start %p state %lu\n", con, con->state);
  1803. more:
  1804. dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
  1805. /* open the socket first? */
  1806. if (con->state == CON_STATE_PREOPEN) {
  1807. BUG_ON(con->sock);
  1808. con->state = CON_STATE_CONNECTING;
  1809. con_out_kvec_reset(con);
  1810. prepare_write_banner(con);
  1811. prepare_read_banner(con);
  1812. BUG_ON(con->in_msg);
  1813. con->in_tag = CEPH_MSGR_TAG_READY;
  1814. dout("try_write initiating connect on %p new state %lu\n",
  1815. con, con->state);
  1816. ret = ceph_tcp_connect(con);
  1817. if (ret < 0) {
  1818. con->error_msg = "connect error";
  1819. goto out;
  1820. }
  1821. }
  1822. more_kvec:
  1823. /* kvec data queued? */
  1824. if (con->out_skip) {
  1825. ret = write_partial_skip(con);
  1826. if (ret <= 0)
  1827. goto out;
  1828. }
  1829. if (con->out_kvec_left) {
  1830. ret = write_partial_kvec(con);
  1831. if (ret <= 0)
  1832. goto out;
  1833. }
  1834. /* msg pages? */
  1835. if (con->out_msg) {
  1836. if (con->out_msg_done) {
  1837. ceph_msg_put(con->out_msg);
  1838. con->out_msg = NULL; /* we're done with this one */
  1839. goto do_next;
  1840. }
  1841. ret = write_partial_message_data(con);
  1842. if (ret == 1)
  1843. goto more_kvec; /* we need to send the footer, too! */
  1844. if (ret == 0)
  1845. goto out;
  1846. if (ret < 0) {
  1847. dout("try_write write_partial_message_data err %d\n",
  1848. ret);
  1849. goto out;
  1850. }
  1851. }
  1852. do_next:
  1853. if (con->state == CON_STATE_OPEN) {
  1854. /* is anything else pending? */
  1855. if (!list_empty(&con->out_queue)) {
  1856. prepare_write_message(con);
  1857. goto more;
  1858. }
  1859. if (con->in_seq > con->in_seq_acked) {
  1860. prepare_write_ack(con);
  1861. goto more;
  1862. }
  1863. if (con_flag_test_and_clear(con, CON_FLAG_KEEPALIVE_PENDING)) {
  1864. prepare_write_keepalive(con);
  1865. goto more;
  1866. }
  1867. }
  1868. /* Nothing to do! */
  1869. con_flag_clear(con, CON_FLAG_WRITE_PENDING);
  1870. dout("try_write nothing else to write.\n");
  1871. ret = 0;
  1872. out:
  1873. dout("try_write done on %p ret %d\n", con, ret);
  1874. return ret;
  1875. }
  1876. /*
  1877. * Read what we can from the socket.
  1878. */
  1879. static int try_read(struct ceph_connection *con)
  1880. {
  1881. int ret = -1;
  1882. more:
  1883. dout("try_read start on %p state %lu\n", con, con->state);
  1884. if (con->state != CON_STATE_CONNECTING &&
  1885. con->state != CON_STATE_NEGOTIATING &&
  1886. con->state != CON_STATE_OPEN)
  1887. return 0;
  1888. BUG_ON(!con->sock);
  1889. dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
  1890. con->in_base_pos);
  1891. if (con->state == CON_STATE_CONNECTING) {
  1892. dout("try_read connecting\n");
  1893. ret = read_partial_banner(con);
  1894. if (ret <= 0)
  1895. goto out;
  1896. ret = process_banner(con);
  1897. if (ret < 0)
  1898. goto out;
  1899. con->state = CON_STATE_NEGOTIATING;
  1900. /*
  1901. * Received banner is good, exchange connection info.
  1902. * Do not reset out_kvec, as sending our banner raced
  1903. * with receiving peer banner after connect completed.
  1904. */
  1905. ret = prepare_write_connect(con);
  1906. if (ret < 0)
  1907. goto out;
  1908. prepare_read_connect(con);
  1909. /* Send connection info before awaiting response */
  1910. goto out;
  1911. }
  1912. if (con->state == CON_STATE_NEGOTIATING) {
  1913. dout("try_read negotiating\n");
  1914. ret = read_partial_connect(con);
  1915. if (ret <= 0)
  1916. goto out;
  1917. ret = process_connect(con);
  1918. if (ret < 0)
  1919. goto out;
  1920. goto more;
  1921. }
  1922. WARN_ON(con->state != CON_STATE_OPEN);
  1923. if (con->in_base_pos < 0) {
  1924. /*
  1925. * skipping + discarding content.
  1926. *
  1927. * FIXME: there must be a better way to do this!
  1928. */
  1929. static char buf[SKIP_BUF_SIZE];
  1930. int skip = min((int) sizeof (buf), -con->in_base_pos);
  1931. dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
  1932. ret = ceph_tcp_recvmsg(con->sock, buf, skip);
  1933. if (ret <= 0)
  1934. goto out;
  1935. con->in_base_pos += ret;
  1936. if (con->in_base_pos)
  1937. goto more;
  1938. }
  1939. if (con->in_tag == CEPH_MSGR_TAG_READY) {
  1940. /*
  1941. * what's next?
  1942. */
  1943. ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
  1944. if (ret <= 0)
  1945. goto out;
  1946. dout("try_read got tag %d\n", (int)con->in_tag);
  1947. switch (con->in_tag) {
  1948. case CEPH_MSGR_TAG_MSG:
  1949. prepare_read_message(con);
  1950. break;
  1951. case CEPH_MSGR_TAG_ACK:
  1952. prepare_read_ack(con);
  1953. break;
  1954. case CEPH_MSGR_TAG_CLOSE:
  1955. con_close_socket(con);
  1956. con->state = CON_STATE_CLOSED;
  1957. goto out;
  1958. default:
  1959. goto bad_tag;
  1960. }
  1961. }
  1962. if (con->in_tag == CEPH_MSGR_TAG_MSG) {
  1963. ret = read_partial_message(con);
  1964. if (ret <= 0) {
  1965. switch (ret) {
  1966. case -EBADMSG:
  1967. con->error_msg = "bad crc";
  1968. ret = -EIO;
  1969. break;
  1970. case -EIO:
  1971. con->error_msg = "io error";
  1972. break;
  1973. }
  1974. goto out;
  1975. }
  1976. if (con->in_tag == CEPH_MSGR_TAG_READY)
  1977. goto more;
  1978. process_message(con);
  1979. if (con->state == CON_STATE_OPEN)
  1980. prepare_read_tag(con);
  1981. goto more;
  1982. }
  1983. if (con->in_tag == CEPH_MSGR_TAG_ACK) {
  1984. ret = read_partial_ack(con);
  1985. if (ret <= 0)
  1986. goto out;
  1987. process_ack(con);
  1988. goto more;
  1989. }
  1990. out:
  1991. dout("try_read done on %p ret %d\n", con, ret);
  1992. return ret;
  1993. bad_tag:
  1994. pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
  1995. con->error_msg = "protocol error, garbage tag";
  1996. ret = -1;
  1997. goto out;
  1998. }
  1999. /*
  2000. * Atomically queue work on a connection after the specified delay.
  2001. * Bump @con reference to avoid races with connection teardown.
  2002. * Returns 0 if work was queued, or an error code otherwise.
  2003. */
  2004. static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
  2005. {
  2006. if (!con->ops->get(con)) {
  2007. dout("%s %p ref count 0\n", __func__, con);
  2008. return -ENOENT;
  2009. }
  2010. if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
  2011. dout("%s %p - already queued\n", __func__, con);
  2012. con->ops->put(con);
  2013. return -EBUSY;
  2014. }
  2015. dout("%s %p %lu\n", __func__, con, delay);
  2016. return 0;
  2017. }
  2018. static void queue_con(struct ceph_connection *con)
  2019. {
  2020. (void) queue_con_delay(con, 0);
  2021. }
  2022. static bool con_sock_closed(struct ceph_connection *con)
  2023. {
  2024. if (!con_flag_test_and_clear(con, CON_FLAG_SOCK_CLOSED))
  2025. return false;
  2026. #define CASE(x) \
  2027. case CON_STATE_ ## x: \
  2028. con->error_msg = "socket closed (con state " #x ")"; \
  2029. break;
  2030. switch (con->state) {
  2031. CASE(CLOSED);
  2032. CASE(PREOPEN);
  2033. CASE(CONNECTING);
  2034. CASE(NEGOTIATING);
  2035. CASE(OPEN);
  2036. CASE(STANDBY);
  2037. default:
  2038. pr_warning("%s con %p unrecognized state %lu\n",
  2039. __func__, con, con->state);
  2040. con->error_msg = "unrecognized con state";
  2041. BUG();
  2042. break;
  2043. }
  2044. #undef CASE
  2045. return true;
  2046. }
  2047. static bool con_backoff(struct ceph_connection *con)
  2048. {
  2049. int ret;
  2050. if (!con_flag_test_and_clear(con, CON_FLAG_BACKOFF))
  2051. return false;
  2052. ret = queue_con_delay(con, round_jiffies_relative(con->delay));
  2053. if (ret) {
  2054. dout("%s: con %p FAILED to back off %lu\n", __func__,
  2055. con, con->delay);
  2056. BUG_ON(ret == -ENOENT);
  2057. con_flag_set(con, CON_FLAG_BACKOFF);
  2058. }
  2059. return true;
  2060. }
  2061. /* Finish fault handling; con->mutex must *not* be held here */
  2062. static void con_fault_finish(struct ceph_connection *con)
  2063. {
  2064. /*
  2065. * in case we faulted due to authentication, invalidate our
  2066. * current tickets so that we can get new ones.
  2067. */
  2068. if (con->auth_retry && con->ops->invalidate_authorizer) {
  2069. dout("calling invalidate_authorizer()\n");
  2070. con->ops->invalidate_authorizer(con);
  2071. }
  2072. if (con->ops->fault)
  2073. con->ops->fault(con);
  2074. }
  2075. /*
  2076. * Do some work on a connection. Drop a connection ref when we're done.
  2077. */
  2078. static void con_work(struct work_struct *work)
  2079. {
  2080. struct ceph_connection *con = container_of(work, struct ceph_connection,
  2081. work.work);
  2082. bool fault;
  2083. mutex_lock(&con->mutex);
  2084. while (true) {
  2085. int ret;
  2086. if ((fault = con_sock_closed(con))) {
  2087. dout("%s: con %p SOCK_CLOSED\n", __func__, con);
  2088. break;
  2089. }
  2090. if (con_backoff(con)) {
  2091. dout("%s: con %p BACKOFF\n", __func__, con);
  2092. break;
  2093. }
  2094. if (con->state == CON_STATE_STANDBY) {
  2095. dout("%s: con %p STANDBY\n", __func__, con);
  2096. break;
  2097. }
  2098. if (con->state == CON_STATE_CLOSED) {
  2099. dout("%s: con %p CLOSED\n", __func__, con);
  2100. BUG_ON(con->sock);
  2101. break;
  2102. }
  2103. if (con->state == CON_STATE_PREOPEN) {
  2104. dout("%s: con %p PREOPEN\n", __func__, con);
  2105. BUG_ON(con->sock);
  2106. }
  2107. ret = try_read(con);
  2108. if (ret < 0) {
  2109. if (ret == -EAGAIN)
  2110. continue;
  2111. con->error_msg = "socket error on read";
  2112. fault = true;
  2113. break;
  2114. }
  2115. ret = try_write(con);
  2116. if (ret < 0) {
  2117. if (ret == -EAGAIN)
  2118. continue;
  2119. con->error_msg = "socket error on write";
  2120. fault = true;
  2121. }
  2122. break; /* If we make it to here, we're done */
  2123. }
  2124. if (fault)
  2125. con_fault(con);
  2126. mutex_unlock(&con->mutex);
  2127. if (fault)
  2128. con_fault_finish(con);
  2129. con->ops->put(con);
  2130. }
  2131. /*
  2132. * Generic error/fault handler. A retry mechanism is used with
  2133. * exponential backoff
  2134. */
  2135. static void con_fault(struct ceph_connection *con)
  2136. {
  2137. pr_warning("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
  2138. ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
  2139. dout("fault %p state %lu to peer %s\n",
  2140. con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
  2141. WARN_ON(con->state != CON_STATE_CONNECTING &&
  2142. con->state != CON_STATE_NEGOTIATING &&
  2143. con->state != CON_STATE_OPEN);
  2144. con_close_socket(con);
  2145. if (con_flag_test(con, CON_FLAG_LOSSYTX)) {
  2146. dout("fault on LOSSYTX channel, marking CLOSED\n");
  2147. con->state = CON_STATE_CLOSED;
  2148. return;
  2149. }
  2150. if (con->in_msg) {
  2151. BUG_ON(con->in_msg->con != con);
  2152. con->in_msg->con = NULL;
  2153. ceph_msg_put(con->in_msg);
  2154. con->in_msg = NULL;
  2155. con->ops->put(con);
  2156. }
  2157. /* Requeue anything that hasn't been acked */
  2158. list_splice_init(&con->out_sent, &con->out_queue);
  2159. /* If there are no messages queued or keepalive pending, place
  2160. * the connection in a STANDBY state */
  2161. if (list_empty(&con->out_queue) &&
  2162. !con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING)) {
  2163. dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
  2164. con_flag_clear(con, CON_FLAG_WRITE_PENDING);
  2165. con->state = CON_STATE_STANDBY;
  2166. } else {
  2167. /* retry after a delay. */
  2168. con->state = CON_STATE_PREOPEN;
  2169. if (con->delay == 0)
  2170. con->delay = BASE_DELAY_INTERVAL;
  2171. else if (con->delay < MAX_DELAY_INTERVAL)
  2172. con->delay *= 2;
  2173. con_flag_set(con, CON_FLAG_BACKOFF);
  2174. queue_con(con);
  2175. }
  2176. }
  2177. /*
  2178. * initialize a new messenger instance
  2179. */
  2180. void ceph_messenger_init(struct ceph_messenger *msgr,
  2181. struct ceph_entity_addr *myaddr,
  2182. u32 supported_features,
  2183. u32 required_features,
  2184. bool nocrc)
  2185. {
  2186. msgr->supported_features = supported_features;
  2187. msgr->required_features = required_features;
  2188. spin_lock_init(&msgr->global_seq_lock);
  2189. if (myaddr)
  2190. msgr->inst.addr = *myaddr;
  2191. /* select a random nonce */
  2192. msgr->inst.addr.type = 0;
  2193. get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
  2194. encode_my_addr(msgr);
  2195. msgr->nocrc = nocrc;
  2196. atomic_set(&msgr->stopping, 0);
  2197. dout("%s %p\n", __func__, msgr);
  2198. }
  2199. EXPORT_SYMBOL(ceph_messenger_init);
  2200. static void clear_standby(struct ceph_connection *con)
  2201. {
  2202. /* come back from STANDBY? */
  2203. if (con->state == CON_STATE_STANDBY) {
  2204. dout("clear_standby %p and ++connect_seq\n", con);
  2205. con->state = CON_STATE_PREOPEN;
  2206. con->connect_seq++;
  2207. WARN_ON(con_flag_test(con, CON_FLAG_WRITE_PENDING));
  2208. WARN_ON(con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING));
  2209. }
  2210. }
  2211. /*
  2212. * Queue up an outgoing message on the given connection.
  2213. */
  2214. void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
  2215. {
  2216. /* set src+dst */
  2217. msg->hdr.src = con->msgr->inst.name;
  2218. BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
  2219. msg->needs_out_seq = true;
  2220. mutex_lock(&con->mutex);
  2221. if (con->state == CON_STATE_CLOSED) {
  2222. dout("con_send %p closed, dropping %p\n", con, msg);
  2223. ceph_msg_put(msg);
  2224. mutex_unlock(&con->mutex);
  2225. return;
  2226. }
  2227. BUG_ON(msg->con != NULL);
  2228. msg->con = con->ops->get(con);
  2229. BUG_ON(msg->con == NULL);
  2230. BUG_ON(!list_empty(&msg->list_head));
  2231. list_add_tail(&msg->list_head, &con->out_queue);
  2232. dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
  2233. ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
  2234. ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
  2235. le32_to_cpu(msg->hdr.front_len),
  2236. le32_to_cpu(msg->hdr.middle_len),
  2237. le32_to_cpu(msg->hdr.data_len));
  2238. clear_standby(con);
  2239. mutex_unlock(&con->mutex);
  2240. /* if there wasn't anything waiting to send before, queue
  2241. * new work */
  2242. if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
  2243. queue_con(con);
  2244. }
  2245. EXPORT_SYMBOL(ceph_con_send);
  2246. /*
  2247. * Revoke a message that was previously queued for send
  2248. */
  2249. void ceph_msg_revoke(struct ceph_msg *msg)
  2250. {
  2251. struct ceph_connection *con = msg->con;
  2252. if (!con)
  2253. return; /* Message not in our possession */
  2254. mutex_lock(&con->mutex);
  2255. if (!list_empty(&msg->list_head)) {
  2256. dout("%s %p msg %p - was on queue\n", __func__, con, msg);
  2257. list_del_init(&msg->list_head);
  2258. BUG_ON(msg->con == NULL);
  2259. msg->con->ops->put(msg->con);
  2260. msg->con = NULL;
  2261. msg->hdr.seq = 0;
  2262. ceph_msg_put(msg);
  2263. }
  2264. if (con->out_msg == msg) {
  2265. dout("%s %p msg %p - was sending\n", __func__, con, msg);
  2266. con->out_msg = NULL;
  2267. if (con->out_kvec_is_msg) {
  2268. con->out_skip = con->out_kvec_bytes;
  2269. con->out_kvec_is_msg = false;
  2270. }
  2271. msg->hdr.seq = 0;
  2272. ceph_msg_put(msg);
  2273. }
  2274. mutex_unlock(&con->mutex);
  2275. }
  2276. /*
  2277. * Revoke a message that we may be reading data into
  2278. */
  2279. void ceph_msg_revoke_incoming(struct ceph_msg *msg)
  2280. {
  2281. struct ceph_connection *con;
  2282. BUG_ON(msg == NULL);
  2283. if (!msg->con) {
  2284. dout("%s msg %p null con\n", __func__, msg);
  2285. return; /* Message not in our possession */
  2286. }
  2287. con = msg->con;
  2288. mutex_lock(&con->mutex);
  2289. if (con->in_msg == msg) {
  2290. unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
  2291. unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
  2292. unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
  2293. /* skip rest of message */
  2294. dout("%s %p msg %p revoked\n", __func__, con, msg);
  2295. con->in_base_pos = con->in_base_pos -
  2296. sizeof(struct ceph_msg_header) -
  2297. front_len -
  2298. middle_len -
  2299. data_len -
  2300. sizeof(struct ceph_msg_footer);
  2301. ceph_msg_put(con->in_msg);
  2302. con->in_msg = NULL;
  2303. con->in_tag = CEPH_MSGR_TAG_READY;
  2304. con->in_seq++;
  2305. } else {
  2306. dout("%s %p in_msg %p msg %p no-op\n",
  2307. __func__, con, con->in_msg, msg);
  2308. }
  2309. mutex_unlock(&con->mutex);
  2310. }
  2311. /*
  2312. * Queue a keepalive byte to ensure the tcp connection is alive.
  2313. */
  2314. void ceph_con_keepalive(struct ceph_connection *con)
  2315. {
  2316. dout("con_keepalive %p\n", con);
  2317. mutex_lock(&con->mutex);
  2318. clear_standby(con);
  2319. mutex_unlock(&con->mutex);
  2320. if (con_flag_test_and_set(con, CON_FLAG_KEEPALIVE_PENDING) == 0 &&
  2321. con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
  2322. queue_con(con);
  2323. }
  2324. EXPORT_SYMBOL(ceph_con_keepalive);
  2325. void ceph_msg_data_set_pages(struct ceph_msg *msg, struct page **pages,
  2326. size_t length, size_t alignment)
  2327. {
  2328. BUG_ON(!pages);
  2329. BUG_ON(!length);
  2330. BUG_ON(msg->pages);
  2331. BUG_ON(msg->length);
  2332. msg->pages = pages;
  2333. msg->length = length;
  2334. msg->page_alignment = alignment & ~PAGE_MASK;
  2335. }
  2336. EXPORT_SYMBOL(ceph_msg_data_set_pages);
  2337. void ceph_msg_data_set_pagelist(struct ceph_msg *msg,
  2338. struct ceph_pagelist *pagelist)
  2339. {
  2340. BUG_ON(!pagelist);
  2341. BUG_ON(!pagelist->length);
  2342. BUG_ON(msg->pagelist);
  2343. msg->pagelist = pagelist;
  2344. }
  2345. EXPORT_SYMBOL(ceph_msg_data_set_pagelist);
  2346. void ceph_msg_data_set_bio(struct ceph_msg *msg, struct bio *bio)
  2347. {
  2348. BUG_ON(!bio);
  2349. BUG_ON(msg->bio);
  2350. msg->bio = bio;
  2351. }
  2352. EXPORT_SYMBOL(ceph_msg_data_set_bio);
  2353. void ceph_msg_data_set_trail(struct ceph_msg *msg, struct ceph_pagelist *trail)
  2354. {
  2355. BUG_ON(!trail);
  2356. BUG_ON(!trail->length);
  2357. BUG_ON(msg->trail);
  2358. msg->trail = trail;
  2359. }
  2360. EXPORT_SYMBOL(ceph_msg_data_set_trail);
  2361. /*
  2362. * construct a new message with given type, size
  2363. * the new msg has a ref count of 1.
  2364. */
  2365. struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
  2366. bool can_fail)
  2367. {
  2368. struct ceph_msg *m;
  2369. m = kzalloc(sizeof(*m), flags);
  2370. if (m == NULL)
  2371. goto out;
  2372. m->hdr.type = cpu_to_le16(type);
  2373. m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
  2374. m->hdr.front_len = cpu_to_le32(front_len);
  2375. INIT_LIST_HEAD(&m->list_head);
  2376. kref_init(&m->kref);
  2377. /* front */
  2378. m->front_max = front_len;
  2379. if (front_len) {
  2380. if (front_len > PAGE_CACHE_SIZE) {
  2381. m->front.iov_base = __vmalloc(front_len, flags,
  2382. PAGE_KERNEL);
  2383. m->front_is_vmalloc = true;
  2384. } else {
  2385. m->front.iov_base = kmalloc(front_len, flags);
  2386. }
  2387. if (m->front.iov_base == NULL) {
  2388. dout("ceph_msg_new can't allocate %d bytes\n",
  2389. front_len);
  2390. goto out2;
  2391. }
  2392. } else {
  2393. m->front.iov_base = NULL;
  2394. }
  2395. m->front.iov_len = front_len;
  2396. dout("ceph_msg_new %p front %d\n", m, front_len);
  2397. return m;
  2398. out2:
  2399. ceph_msg_put(m);
  2400. out:
  2401. if (!can_fail) {
  2402. pr_err("msg_new can't create type %d front %d\n", type,
  2403. front_len);
  2404. WARN_ON(1);
  2405. } else {
  2406. dout("msg_new can't create type %d front %d\n", type,
  2407. front_len);
  2408. }
  2409. return NULL;
  2410. }
  2411. EXPORT_SYMBOL(ceph_msg_new);
  2412. /*
  2413. * Allocate "middle" portion of a message, if it is needed and wasn't
  2414. * allocated by alloc_msg. This allows us to read a small fixed-size
  2415. * per-type header in the front and then gracefully fail (i.e.,
  2416. * propagate the error to the caller based on info in the front) when
  2417. * the middle is too large.
  2418. */
  2419. static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
  2420. {
  2421. int type = le16_to_cpu(msg->hdr.type);
  2422. int middle_len = le32_to_cpu(msg->hdr.middle_len);
  2423. dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
  2424. ceph_msg_type_name(type), middle_len);
  2425. BUG_ON(!middle_len);
  2426. BUG_ON(msg->middle);
  2427. msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
  2428. if (!msg->middle)
  2429. return -ENOMEM;
  2430. return 0;
  2431. }
  2432. /*
  2433. * Allocate a message for receiving an incoming message on a
  2434. * connection, and save the result in con->in_msg. Uses the
  2435. * connection's private alloc_msg op if available.
  2436. *
  2437. * Returns 0 on success, or a negative error code.
  2438. *
  2439. * On success, if we set *skip = 1:
  2440. * - the next message should be skipped and ignored.
  2441. * - con->in_msg == NULL
  2442. * or if we set *skip = 0:
  2443. * - con->in_msg is non-null.
  2444. * On error (ENOMEM, EAGAIN, ...),
  2445. * - con->in_msg == NULL
  2446. */
  2447. static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip)
  2448. {
  2449. struct ceph_msg_header *hdr = &con->in_hdr;
  2450. int middle_len = le32_to_cpu(hdr->middle_len);
  2451. struct ceph_msg *msg;
  2452. int ret = 0;
  2453. BUG_ON(con->in_msg != NULL);
  2454. BUG_ON(!con->ops->alloc_msg);
  2455. mutex_unlock(&con->mutex);
  2456. msg = con->ops->alloc_msg(con, hdr, skip);
  2457. mutex_lock(&con->mutex);
  2458. if (con->state != CON_STATE_OPEN) {
  2459. if (msg)
  2460. ceph_msg_put(msg);
  2461. return -EAGAIN;
  2462. }
  2463. if (msg) {
  2464. BUG_ON(*skip);
  2465. con->in_msg = msg;
  2466. con->in_msg->con = con->ops->get(con);
  2467. BUG_ON(con->in_msg->con == NULL);
  2468. } else {
  2469. /*
  2470. * Null message pointer means either we should skip
  2471. * this message or we couldn't allocate memory. The
  2472. * former is not an error.
  2473. */
  2474. if (*skip)
  2475. return 0;
  2476. con->error_msg = "error allocating memory for incoming message";
  2477. return -ENOMEM;
  2478. }
  2479. memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
  2480. if (middle_len && !con->in_msg->middle) {
  2481. ret = ceph_alloc_middle(con, con->in_msg);
  2482. if (ret < 0) {
  2483. ceph_msg_put(con->in_msg);
  2484. con->in_msg = NULL;
  2485. }
  2486. }
  2487. return ret;
  2488. }
  2489. /*
  2490. * Free a generically kmalloc'd message.
  2491. */
  2492. void ceph_msg_kfree(struct ceph_msg *m)
  2493. {
  2494. dout("msg_kfree %p\n", m);
  2495. if (m->front_is_vmalloc)
  2496. vfree(m->front.iov_base);
  2497. else
  2498. kfree(m->front.iov_base);
  2499. kfree(m);
  2500. }
  2501. /*
  2502. * Drop a msg ref. Destroy as needed.
  2503. */
  2504. void ceph_msg_last_put(struct kref *kref)
  2505. {
  2506. struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
  2507. dout("ceph_msg_put last one on %p\n", m);
  2508. WARN_ON(!list_empty(&m->list_head));
  2509. /* drop middle, data, if any */
  2510. if (m->middle) {
  2511. ceph_buffer_put(m->middle);
  2512. m->middle = NULL;
  2513. }
  2514. m->length = 0;
  2515. m->pages = NULL;
  2516. if (m->pagelist) {
  2517. ceph_pagelist_release(m->pagelist);
  2518. kfree(m->pagelist);
  2519. m->pagelist = NULL;
  2520. }
  2521. m->trail = NULL;
  2522. if (m->pool)
  2523. ceph_msgpool_put(m->pool, m);
  2524. else
  2525. ceph_msg_kfree(m);
  2526. }
  2527. EXPORT_SYMBOL(ceph_msg_last_put);
  2528. void ceph_msg_dump(struct ceph_msg *msg)
  2529. {
  2530. pr_debug("msg_dump %p (front_max %d length %zd)\n", msg,
  2531. msg->front_max, msg->length);
  2532. print_hex_dump(KERN_DEBUG, "header: ",
  2533. DUMP_PREFIX_OFFSET, 16, 1,
  2534. &msg->hdr, sizeof(msg->hdr), true);
  2535. print_hex_dump(KERN_DEBUG, " front: ",
  2536. DUMP_PREFIX_OFFSET, 16, 1,
  2537. msg->front.iov_base, msg->front.iov_len, true);
  2538. if (msg->middle)
  2539. print_hex_dump(KERN_DEBUG, "middle: ",
  2540. DUMP_PREFIX_OFFSET, 16, 1,
  2541. msg->middle->vec.iov_base,
  2542. msg->middle->vec.iov_len, true);
  2543. print_hex_dump(KERN_DEBUG, "footer: ",
  2544. DUMP_PREFIX_OFFSET, 16, 1,
  2545. &msg->footer, sizeof(msg->footer), true);
  2546. }
  2547. EXPORT_SYMBOL(ceph_msg_dump);