messenger.c 88 KB

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