messenger.c 88 KB

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