messenger.c 86 KB

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