messenger.c 87 KB

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