messenger.c 85 KB

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