messenger.c 72 KB

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