messenger.c 89 KB

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