messenger.c 85 KB

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