messenger.c 73 KB

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