messenger.c 89 KB

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