messenger.c 86 KB

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