messenger.c 84 KB

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