messenger.c 71 KB

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