messenger.c 85 KB

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