tcp.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. *
  3. * vim: noexpandtab sw=8 ts=8 sts=0:
  4. *
  5. * Copyright (C) 2004 Oracle. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with this program; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 021110-1307, USA.
  21. *
  22. * ----
  23. *
  24. * Callers for this were originally written against a very simple synchronus
  25. * API. This implementation reflects those simple callers. Some day I'm sure
  26. * we'll need to move to a more robust posting/callback mechanism.
  27. *
  28. * Transmit calls pass in kernel virtual addresses and block copying this into
  29. * the socket's tx buffers via a usual blocking sendmsg. They'll block waiting
  30. * for a failed socket to timeout. TX callers can also pass in a poniter to an
  31. * 'int' which gets filled with an errno off the wire in response to the
  32. * message they send.
  33. *
  34. * Handlers for unsolicited messages are registered. Each socket has a page
  35. * that incoming data is copied into. First the header, then the data.
  36. * Handlers are called from only one thread with a reference to this per-socket
  37. * page. This page is destroyed after the handler call, so it can't be
  38. * referenced beyond the call. Handlers may block but are discouraged from
  39. * doing so.
  40. *
  41. * Any framing errors (bad magic, large payload lengths) close a connection.
  42. *
  43. * Our sock_container holds the state we associate with a socket. It's current
  44. * framing state is held there as well as the refcounting we do around when it
  45. * is safe to tear down the socket. The socket is only finally torn down from
  46. * the container when the container loses all of its references -- so as long
  47. * as you hold a ref on the container you can trust that the socket is valid
  48. * for use with kernel socket APIs.
  49. *
  50. * Connections are initiated between a pair of nodes when the node with the
  51. * higher node number gets a heartbeat callback which indicates that the lower
  52. * numbered node has started heartbeating. The lower numbered node is passive
  53. * and only accepts the connection if the higher numbered node is heartbeating.
  54. */
  55. #include <linux/kernel.h>
  56. #include <linux/jiffies.h>
  57. #include <linux/slab.h>
  58. #include <linux/idr.h>
  59. #include <linux/kref.h>
  60. #include <linux/net.h>
  61. #include <linux/export.h>
  62. #include <net/tcp.h>
  63. #include <asm/uaccess.h>
  64. #include "heartbeat.h"
  65. #include "tcp.h"
  66. #include "nodemanager.h"
  67. #define MLOG_MASK_PREFIX ML_TCP
  68. #include "masklog.h"
  69. #include "quorum.h"
  70. #include "tcp_internal.h"
  71. #define SC_NODEF_FMT "node %s (num %u) at %pI4:%u"
  72. #define SC_NODEF_ARGS(sc) sc->sc_node->nd_name, sc->sc_node->nd_num, \
  73. &sc->sc_node->nd_ipv4_address, \
  74. ntohs(sc->sc_node->nd_ipv4_port)
  75. /*
  76. * In the following two log macros, the whitespace after the ',' just
  77. * before ##args is intentional. Otherwise, gcc 2.95 will eat the
  78. * previous token if args expands to nothing.
  79. */
  80. #define msglog(hdr, fmt, args...) do { \
  81. typeof(hdr) __hdr = (hdr); \
  82. mlog(ML_MSG, "[mag %u len %u typ %u stat %d sys_stat %d " \
  83. "key %08x num %u] " fmt, \
  84. be16_to_cpu(__hdr->magic), be16_to_cpu(__hdr->data_len), \
  85. be16_to_cpu(__hdr->msg_type), be32_to_cpu(__hdr->status), \
  86. be32_to_cpu(__hdr->sys_status), be32_to_cpu(__hdr->key), \
  87. be32_to_cpu(__hdr->msg_num) , ##args); \
  88. } while (0)
  89. #define sclog(sc, fmt, args...) do { \
  90. typeof(sc) __sc = (sc); \
  91. mlog(ML_SOCKET, "[sc %p refs %d sock %p node %u page %p " \
  92. "pg_off %zu] " fmt, __sc, \
  93. atomic_read(&__sc->sc_kref.refcount), __sc->sc_sock, \
  94. __sc->sc_node->nd_num, __sc->sc_page, __sc->sc_page_off , \
  95. ##args); \
  96. } while (0)
  97. static DEFINE_RWLOCK(o2net_handler_lock);
  98. static struct rb_root o2net_handler_tree = RB_ROOT;
  99. static struct o2net_node o2net_nodes[O2NM_MAX_NODES];
  100. /* XXX someday we'll need better accounting */
  101. static struct socket *o2net_listen_sock;
  102. /*
  103. * listen work is only queued by the listening socket callbacks on the
  104. * o2net_wq. teardown detaches the callbacks before destroying the workqueue.
  105. * quorum work is queued as sock containers are shutdown.. stop_listening
  106. * tears down all the node's sock containers, preventing future shutdowns
  107. * and queued quroum work, before canceling delayed quorum work and
  108. * destroying the work queue.
  109. */
  110. static struct workqueue_struct *o2net_wq;
  111. static struct work_struct o2net_listen_work;
  112. static struct o2hb_callback_func o2net_hb_up, o2net_hb_down;
  113. #define O2NET_HB_PRI 0x1
  114. static struct o2net_handshake *o2net_hand;
  115. static struct o2net_msg *o2net_keep_req, *o2net_keep_resp;
  116. static int o2net_sys_err_translations[O2NET_ERR_MAX] =
  117. {[O2NET_ERR_NONE] = 0,
  118. [O2NET_ERR_NO_HNDLR] = -ENOPROTOOPT,
  119. [O2NET_ERR_OVERFLOW] = -EOVERFLOW,
  120. [O2NET_ERR_DIED] = -EHOSTDOWN,};
  121. /* can't quite avoid *all* internal declarations :/ */
  122. static void o2net_sc_connect_completed(struct work_struct *work);
  123. static void o2net_rx_until_empty(struct work_struct *work);
  124. static void o2net_shutdown_sc(struct work_struct *work);
  125. static void o2net_listen_data_ready(struct sock *sk);
  126. static void o2net_sc_send_keep_req(struct work_struct *work);
  127. static void o2net_idle_timer(unsigned long data);
  128. static void o2net_sc_postpone_idle(struct o2net_sock_container *sc);
  129. static void o2net_sc_reset_idle_timer(struct o2net_sock_container *sc);
  130. #ifdef CONFIG_DEBUG_FS
  131. static void o2net_init_nst(struct o2net_send_tracking *nst, u32 msgtype,
  132. u32 msgkey, struct task_struct *task, u8 node)
  133. {
  134. INIT_LIST_HEAD(&nst->st_net_debug_item);
  135. nst->st_task = task;
  136. nst->st_msg_type = msgtype;
  137. nst->st_msg_key = msgkey;
  138. nst->st_node = node;
  139. }
  140. static inline void o2net_set_nst_sock_time(struct o2net_send_tracking *nst)
  141. {
  142. nst->st_sock_time = ktime_get();
  143. }
  144. static inline void o2net_set_nst_send_time(struct o2net_send_tracking *nst)
  145. {
  146. nst->st_send_time = ktime_get();
  147. }
  148. static inline void o2net_set_nst_status_time(struct o2net_send_tracking *nst)
  149. {
  150. nst->st_status_time = ktime_get();
  151. }
  152. static inline void o2net_set_nst_sock_container(struct o2net_send_tracking *nst,
  153. struct o2net_sock_container *sc)
  154. {
  155. nst->st_sc = sc;
  156. }
  157. static inline void o2net_set_nst_msg_id(struct o2net_send_tracking *nst,
  158. u32 msg_id)
  159. {
  160. nst->st_id = msg_id;
  161. }
  162. static inline void o2net_set_sock_timer(struct o2net_sock_container *sc)
  163. {
  164. sc->sc_tv_timer = ktime_get();
  165. }
  166. static inline void o2net_set_data_ready_time(struct o2net_sock_container *sc)
  167. {
  168. sc->sc_tv_data_ready = ktime_get();
  169. }
  170. static inline void o2net_set_advance_start_time(struct o2net_sock_container *sc)
  171. {
  172. sc->sc_tv_advance_start = ktime_get();
  173. }
  174. static inline void o2net_set_advance_stop_time(struct o2net_sock_container *sc)
  175. {
  176. sc->sc_tv_advance_stop = ktime_get();
  177. }
  178. static inline void o2net_set_func_start_time(struct o2net_sock_container *sc)
  179. {
  180. sc->sc_tv_func_start = ktime_get();
  181. }
  182. static inline void o2net_set_func_stop_time(struct o2net_sock_container *sc)
  183. {
  184. sc->sc_tv_func_stop = ktime_get();
  185. }
  186. #else /* CONFIG_DEBUG_FS */
  187. # define o2net_init_nst(a, b, c, d, e)
  188. # define o2net_set_nst_sock_time(a)
  189. # define o2net_set_nst_send_time(a)
  190. # define o2net_set_nst_status_time(a)
  191. # define o2net_set_nst_sock_container(a, b)
  192. # define o2net_set_nst_msg_id(a, b)
  193. # define o2net_set_sock_timer(a)
  194. # define o2net_set_data_ready_time(a)
  195. # define o2net_set_advance_start_time(a)
  196. # define o2net_set_advance_stop_time(a)
  197. # define o2net_set_func_start_time(a)
  198. # define o2net_set_func_stop_time(a)
  199. #endif /* CONFIG_DEBUG_FS */
  200. #ifdef CONFIG_OCFS2_FS_STATS
  201. static ktime_t o2net_get_func_run_time(struct o2net_sock_container *sc)
  202. {
  203. return ktime_sub(sc->sc_tv_func_stop, sc->sc_tv_func_start);
  204. }
  205. static void o2net_update_send_stats(struct o2net_send_tracking *nst,
  206. struct o2net_sock_container *sc)
  207. {
  208. sc->sc_tv_status_total = ktime_add(sc->sc_tv_status_total,
  209. ktime_sub(ktime_get(),
  210. nst->st_status_time));
  211. sc->sc_tv_send_total = ktime_add(sc->sc_tv_send_total,
  212. ktime_sub(nst->st_status_time,
  213. nst->st_send_time));
  214. sc->sc_tv_acquiry_total = ktime_add(sc->sc_tv_acquiry_total,
  215. ktime_sub(nst->st_send_time,
  216. nst->st_sock_time));
  217. sc->sc_send_count++;
  218. }
  219. static void o2net_update_recv_stats(struct o2net_sock_container *sc)
  220. {
  221. sc->sc_tv_process_total = ktime_add(sc->sc_tv_process_total,
  222. o2net_get_func_run_time(sc));
  223. sc->sc_recv_count++;
  224. }
  225. #else
  226. # define o2net_update_send_stats(a, b)
  227. # define o2net_update_recv_stats(sc)
  228. #endif /* CONFIG_OCFS2_FS_STATS */
  229. static inline unsigned int o2net_reconnect_delay(void)
  230. {
  231. return o2nm_single_cluster->cl_reconnect_delay_ms;
  232. }
  233. static inline unsigned int o2net_keepalive_delay(void)
  234. {
  235. return o2nm_single_cluster->cl_keepalive_delay_ms;
  236. }
  237. static inline unsigned int o2net_idle_timeout(void)
  238. {
  239. return o2nm_single_cluster->cl_idle_timeout_ms;
  240. }
  241. static inline int o2net_sys_err_to_errno(enum o2net_system_error err)
  242. {
  243. int trans;
  244. BUG_ON(err >= O2NET_ERR_MAX);
  245. trans = o2net_sys_err_translations[err];
  246. /* Just in case we mess up the translation table above */
  247. BUG_ON(err != O2NET_ERR_NONE && trans == 0);
  248. return trans;
  249. }
  250. static struct o2net_node * o2net_nn_from_num(u8 node_num)
  251. {
  252. BUG_ON(node_num >= ARRAY_SIZE(o2net_nodes));
  253. return &o2net_nodes[node_num];
  254. }
  255. static u8 o2net_num_from_nn(struct o2net_node *nn)
  256. {
  257. BUG_ON(nn == NULL);
  258. return nn - o2net_nodes;
  259. }
  260. /* ------------------------------------------------------------ */
  261. static int o2net_prep_nsw(struct o2net_node *nn, struct o2net_status_wait *nsw)
  262. {
  263. int ret;
  264. spin_lock(&nn->nn_lock);
  265. ret = idr_alloc(&nn->nn_status_idr, nsw, 0, 0, GFP_ATOMIC);
  266. if (ret >= 0) {
  267. nsw->ns_id = ret;
  268. list_add_tail(&nsw->ns_node_item, &nn->nn_status_list);
  269. }
  270. spin_unlock(&nn->nn_lock);
  271. if (ret < 0)
  272. return ret;
  273. init_waitqueue_head(&nsw->ns_wq);
  274. nsw->ns_sys_status = O2NET_ERR_NONE;
  275. nsw->ns_status = 0;
  276. return 0;
  277. }
  278. static void o2net_complete_nsw_locked(struct o2net_node *nn,
  279. struct o2net_status_wait *nsw,
  280. enum o2net_system_error sys_status,
  281. s32 status)
  282. {
  283. assert_spin_locked(&nn->nn_lock);
  284. if (!list_empty(&nsw->ns_node_item)) {
  285. list_del_init(&nsw->ns_node_item);
  286. nsw->ns_sys_status = sys_status;
  287. nsw->ns_status = status;
  288. idr_remove(&nn->nn_status_idr, nsw->ns_id);
  289. wake_up(&nsw->ns_wq);
  290. }
  291. }
  292. static void o2net_complete_nsw(struct o2net_node *nn,
  293. struct o2net_status_wait *nsw,
  294. u64 id, enum o2net_system_error sys_status,
  295. s32 status)
  296. {
  297. spin_lock(&nn->nn_lock);
  298. if (nsw == NULL) {
  299. if (id > INT_MAX)
  300. goto out;
  301. nsw = idr_find(&nn->nn_status_idr, id);
  302. if (nsw == NULL)
  303. goto out;
  304. }
  305. o2net_complete_nsw_locked(nn, nsw, sys_status, status);
  306. out:
  307. spin_unlock(&nn->nn_lock);
  308. return;
  309. }
  310. static void o2net_complete_nodes_nsw(struct o2net_node *nn)
  311. {
  312. struct o2net_status_wait *nsw, *tmp;
  313. unsigned int num_kills = 0;
  314. assert_spin_locked(&nn->nn_lock);
  315. list_for_each_entry_safe(nsw, tmp, &nn->nn_status_list, ns_node_item) {
  316. o2net_complete_nsw_locked(nn, nsw, O2NET_ERR_DIED, 0);
  317. num_kills++;
  318. }
  319. mlog(0, "completed %d messages for node %u\n", num_kills,
  320. o2net_num_from_nn(nn));
  321. }
  322. static int o2net_nsw_completed(struct o2net_node *nn,
  323. struct o2net_status_wait *nsw)
  324. {
  325. int completed;
  326. spin_lock(&nn->nn_lock);
  327. completed = list_empty(&nsw->ns_node_item);
  328. spin_unlock(&nn->nn_lock);
  329. return completed;
  330. }
  331. /* ------------------------------------------------------------ */
  332. static void sc_kref_release(struct kref *kref)
  333. {
  334. struct o2net_sock_container *sc = container_of(kref,
  335. struct o2net_sock_container, sc_kref);
  336. BUG_ON(timer_pending(&sc->sc_idle_timeout));
  337. sclog(sc, "releasing\n");
  338. if (sc->sc_sock) {
  339. sock_release(sc->sc_sock);
  340. sc->sc_sock = NULL;
  341. }
  342. o2nm_undepend_item(&sc->sc_node->nd_item);
  343. o2nm_node_put(sc->sc_node);
  344. sc->sc_node = NULL;
  345. o2net_debug_del_sc(sc);
  346. if (sc->sc_page)
  347. __free_page(sc->sc_page);
  348. kfree(sc);
  349. }
  350. static void sc_put(struct o2net_sock_container *sc)
  351. {
  352. sclog(sc, "put\n");
  353. kref_put(&sc->sc_kref, sc_kref_release);
  354. }
  355. static void sc_get(struct o2net_sock_container *sc)
  356. {
  357. sclog(sc, "get\n");
  358. kref_get(&sc->sc_kref);
  359. }
  360. static struct o2net_sock_container *sc_alloc(struct o2nm_node *node)
  361. {
  362. struct o2net_sock_container *sc, *ret = NULL;
  363. struct page *page = NULL;
  364. int status = 0;
  365. page = alloc_page(GFP_NOFS);
  366. sc = kzalloc(sizeof(*sc), GFP_NOFS);
  367. if (sc == NULL || page == NULL)
  368. goto out;
  369. kref_init(&sc->sc_kref);
  370. o2nm_node_get(node);
  371. sc->sc_node = node;
  372. /* pin the node item of the remote node */
  373. status = o2nm_depend_item(&node->nd_item);
  374. if (status) {
  375. mlog_errno(status);
  376. o2nm_node_put(node);
  377. goto out;
  378. }
  379. INIT_WORK(&sc->sc_connect_work, o2net_sc_connect_completed);
  380. INIT_WORK(&sc->sc_rx_work, o2net_rx_until_empty);
  381. INIT_WORK(&sc->sc_shutdown_work, o2net_shutdown_sc);
  382. INIT_DELAYED_WORK(&sc->sc_keepalive_work, o2net_sc_send_keep_req);
  383. init_timer(&sc->sc_idle_timeout);
  384. sc->sc_idle_timeout.function = o2net_idle_timer;
  385. sc->sc_idle_timeout.data = (unsigned long)sc;
  386. sclog(sc, "alloced\n");
  387. ret = sc;
  388. sc->sc_page = page;
  389. o2net_debug_add_sc(sc);
  390. sc = NULL;
  391. page = NULL;
  392. out:
  393. if (page)
  394. __free_page(page);
  395. kfree(sc);
  396. return ret;
  397. }
  398. /* ------------------------------------------------------------ */
  399. static void o2net_sc_queue_work(struct o2net_sock_container *sc,
  400. struct work_struct *work)
  401. {
  402. sc_get(sc);
  403. if (!queue_work(o2net_wq, work))
  404. sc_put(sc);
  405. }
  406. static void o2net_sc_queue_delayed_work(struct o2net_sock_container *sc,
  407. struct delayed_work *work,
  408. int delay)
  409. {
  410. sc_get(sc);
  411. if (!queue_delayed_work(o2net_wq, work, delay))
  412. sc_put(sc);
  413. }
  414. static void o2net_sc_cancel_delayed_work(struct o2net_sock_container *sc,
  415. struct delayed_work *work)
  416. {
  417. if (cancel_delayed_work(work))
  418. sc_put(sc);
  419. }
  420. static atomic_t o2net_connected_peers = ATOMIC_INIT(0);
  421. int o2net_num_connected_peers(void)
  422. {
  423. return atomic_read(&o2net_connected_peers);
  424. }
  425. static void o2net_set_nn_state(struct o2net_node *nn,
  426. struct o2net_sock_container *sc,
  427. unsigned valid, int err)
  428. {
  429. int was_valid = nn->nn_sc_valid;
  430. int was_err = nn->nn_persistent_error;
  431. struct o2net_sock_container *old_sc = nn->nn_sc;
  432. assert_spin_locked(&nn->nn_lock);
  433. if (old_sc && !sc)
  434. atomic_dec(&o2net_connected_peers);
  435. else if (!old_sc && sc)
  436. atomic_inc(&o2net_connected_peers);
  437. /* the node num comparison and single connect/accept path should stop
  438. * an non-null sc from being overwritten with another */
  439. BUG_ON(sc && nn->nn_sc && nn->nn_sc != sc);
  440. mlog_bug_on_msg(err && valid, "err %d valid %u\n", err, valid);
  441. mlog_bug_on_msg(valid && !sc, "valid %u sc %p\n", valid, sc);
  442. if (was_valid && !valid && err == 0)
  443. err = -ENOTCONN;
  444. mlog(ML_CONN, "node %u sc: %p -> %p, valid %u -> %u, err %d -> %d\n",
  445. o2net_num_from_nn(nn), nn->nn_sc, sc, nn->nn_sc_valid, valid,
  446. nn->nn_persistent_error, err);
  447. nn->nn_sc = sc;
  448. nn->nn_sc_valid = valid ? 1 : 0;
  449. nn->nn_persistent_error = err;
  450. /* mirrors o2net_tx_can_proceed() */
  451. if (nn->nn_persistent_error || nn->nn_sc_valid)
  452. wake_up(&nn->nn_sc_wq);
  453. if (was_valid && !was_err && nn->nn_persistent_error) {
  454. o2quo_conn_err(o2net_num_from_nn(nn));
  455. queue_delayed_work(o2net_wq, &nn->nn_still_up,
  456. msecs_to_jiffies(O2NET_QUORUM_DELAY_MS));
  457. }
  458. if (was_valid && !valid) {
  459. if (old_sc)
  460. printk(KERN_NOTICE "o2net: No longer connected to "
  461. SC_NODEF_FMT "\n", SC_NODEF_ARGS(old_sc));
  462. o2net_complete_nodes_nsw(nn);
  463. }
  464. if (!was_valid && valid) {
  465. o2quo_conn_up(o2net_num_from_nn(nn));
  466. cancel_delayed_work(&nn->nn_connect_expired);
  467. printk(KERN_NOTICE "o2net: %s " SC_NODEF_FMT "\n",
  468. o2nm_this_node() > sc->sc_node->nd_num ?
  469. "Connected to" : "Accepted connection from",
  470. SC_NODEF_ARGS(sc));
  471. }
  472. /* trigger the connecting worker func as long as we're not valid,
  473. * it will back off if it shouldn't connect. This can be called
  474. * from node config teardown and so needs to be careful about
  475. * the work queue actually being up. */
  476. if (!valid && o2net_wq) {
  477. unsigned long delay;
  478. /* delay if we're within a RECONNECT_DELAY of the
  479. * last attempt */
  480. delay = (nn->nn_last_connect_attempt +
  481. msecs_to_jiffies(o2net_reconnect_delay()))
  482. - jiffies;
  483. if (delay > msecs_to_jiffies(o2net_reconnect_delay()))
  484. delay = 0;
  485. mlog(ML_CONN, "queueing conn attempt in %lu jiffies\n", delay);
  486. queue_delayed_work(o2net_wq, &nn->nn_connect_work, delay);
  487. /*
  488. * Delay the expired work after idle timeout.
  489. *
  490. * We might have lots of failed connection attempts that run
  491. * through here but we only cancel the connect_expired work when
  492. * a connection attempt succeeds. So only the first enqueue of
  493. * the connect_expired work will do anything. The rest will see
  494. * that it's already queued and do nothing.
  495. */
  496. delay += msecs_to_jiffies(o2net_idle_timeout());
  497. queue_delayed_work(o2net_wq, &nn->nn_connect_expired, delay);
  498. }
  499. /* keep track of the nn's sc ref for the caller */
  500. if ((old_sc == NULL) && sc)
  501. sc_get(sc);
  502. if (old_sc && (old_sc != sc)) {
  503. o2net_sc_queue_work(old_sc, &old_sc->sc_shutdown_work);
  504. sc_put(old_sc);
  505. }
  506. }
  507. /* see o2net_register_callbacks() */
  508. static void o2net_data_ready(struct sock *sk)
  509. {
  510. void (*ready)(struct sock *sk);
  511. read_lock(&sk->sk_callback_lock);
  512. if (sk->sk_user_data) {
  513. struct o2net_sock_container *sc = sk->sk_user_data;
  514. sclog(sc, "data_ready hit\n");
  515. o2net_set_data_ready_time(sc);
  516. o2net_sc_queue_work(sc, &sc->sc_rx_work);
  517. ready = sc->sc_data_ready;
  518. } else {
  519. ready = sk->sk_data_ready;
  520. }
  521. read_unlock(&sk->sk_callback_lock);
  522. ready(sk);
  523. }
  524. /* see o2net_register_callbacks() */
  525. static void o2net_state_change(struct sock *sk)
  526. {
  527. void (*state_change)(struct sock *sk);
  528. struct o2net_sock_container *sc;
  529. read_lock(&sk->sk_callback_lock);
  530. sc = sk->sk_user_data;
  531. if (sc == NULL) {
  532. state_change = sk->sk_state_change;
  533. goto out;
  534. }
  535. sclog(sc, "state_change to %d\n", sk->sk_state);
  536. state_change = sc->sc_state_change;
  537. switch(sk->sk_state) {
  538. /* ignore connecting sockets as they make progress */
  539. case TCP_SYN_SENT:
  540. case TCP_SYN_RECV:
  541. break;
  542. case TCP_ESTABLISHED:
  543. o2net_sc_queue_work(sc, &sc->sc_connect_work);
  544. break;
  545. default:
  546. printk(KERN_INFO "o2net: Connection to " SC_NODEF_FMT
  547. " shutdown, state %d\n",
  548. SC_NODEF_ARGS(sc), sk->sk_state);
  549. o2net_sc_queue_work(sc, &sc->sc_shutdown_work);
  550. break;
  551. }
  552. out:
  553. read_unlock(&sk->sk_callback_lock);
  554. state_change(sk);
  555. }
  556. /*
  557. * we register callbacks so we can queue work on events before calling
  558. * the original callbacks. our callbacks our careful to test user_data
  559. * to discover when they've reaced with o2net_unregister_callbacks().
  560. */
  561. static void o2net_register_callbacks(struct sock *sk,
  562. struct o2net_sock_container *sc)
  563. {
  564. write_lock_bh(&sk->sk_callback_lock);
  565. /* accepted sockets inherit the old listen socket data ready */
  566. if (sk->sk_data_ready == o2net_listen_data_ready) {
  567. sk->sk_data_ready = sk->sk_user_data;
  568. sk->sk_user_data = NULL;
  569. }
  570. BUG_ON(sk->sk_user_data != NULL);
  571. sk->sk_user_data = sc;
  572. sc_get(sc);
  573. sc->sc_data_ready = sk->sk_data_ready;
  574. sc->sc_state_change = sk->sk_state_change;
  575. sk->sk_data_ready = o2net_data_ready;
  576. sk->sk_state_change = o2net_state_change;
  577. mutex_init(&sc->sc_send_lock);
  578. write_unlock_bh(&sk->sk_callback_lock);
  579. }
  580. static int o2net_unregister_callbacks(struct sock *sk,
  581. struct o2net_sock_container *sc)
  582. {
  583. int ret = 0;
  584. write_lock_bh(&sk->sk_callback_lock);
  585. if (sk->sk_user_data == sc) {
  586. ret = 1;
  587. sk->sk_user_data = NULL;
  588. sk->sk_data_ready = sc->sc_data_ready;
  589. sk->sk_state_change = sc->sc_state_change;
  590. }
  591. write_unlock_bh(&sk->sk_callback_lock);
  592. return ret;
  593. }
  594. /*
  595. * this is a little helper that is called by callers who have seen a problem
  596. * with an sc and want to detach it from the nn if someone already hasn't beat
  597. * them to it. if an error is given then the shutdown will be persistent
  598. * and pending transmits will be canceled.
  599. */
  600. static void o2net_ensure_shutdown(struct o2net_node *nn,
  601. struct o2net_sock_container *sc,
  602. int err)
  603. {
  604. spin_lock(&nn->nn_lock);
  605. if (nn->nn_sc == sc)
  606. o2net_set_nn_state(nn, NULL, 0, err);
  607. spin_unlock(&nn->nn_lock);
  608. }
  609. /*
  610. * This work queue function performs the blocking parts of socket shutdown. A
  611. * few paths lead here. set_nn_state will trigger this callback if it sees an
  612. * sc detached from the nn. state_change will also trigger this callback
  613. * directly when it sees errors. In that case we need to call set_nn_state
  614. * ourselves as state_change couldn't get the nn_lock and call set_nn_state
  615. * itself.
  616. */
  617. static void o2net_shutdown_sc(struct work_struct *work)
  618. {
  619. struct o2net_sock_container *sc =
  620. container_of(work, struct o2net_sock_container,
  621. sc_shutdown_work);
  622. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  623. sclog(sc, "shutting down\n");
  624. /* drop the callbacks ref and call shutdown only once */
  625. if (o2net_unregister_callbacks(sc->sc_sock->sk, sc)) {
  626. /* we shouldn't flush as we're in the thread, the
  627. * races with pending sc work structs are harmless */
  628. del_timer_sync(&sc->sc_idle_timeout);
  629. o2net_sc_cancel_delayed_work(sc, &sc->sc_keepalive_work);
  630. sc_put(sc);
  631. kernel_sock_shutdown(sc->sc_sock, SHUT_RDWR);
  632. }
  633. /* not fatal so failed connects before the other guy has our
  634. * heartbeat can be retried */
  635. o2net_ensure_shutdown(nn, sc, 0);
  636. sc_put(sc);
  637. }
  638. /* ------------------------------------------------------------ */
  639. static int o2net_handler_cmp(struct o2net_msg_handler *nmh, u32 msg_type,
  640. u32 key)
  641. {
  642. int ret = memcmp(&nmh->nh_key, &key, sizeof(key));
  643. if (ret == 0)
  644. ret = memcmp(&nmh->nh_msg_type, &msg_type, sizeof(msg_type));
  645. return ret;
  646. }
  647. static struct o2net_msg_handler *
  648. o2net_handler_tree_lookup(u32 msg_type, u32 key, struct rb_node ***ret_p,
  649. struct rb_node **ret_parent)
  650. {
  651. struct rb_node **p = &o2net_handler_tree.rb_node;
  652. struct rb_node *parent = NULL;
  653. struct o2net_msg_handler *nmh, *ret = NULL;
  654. int cmp;
  655. while (*p) {
  656. parent = *p;
  657. nmh = rb_entry(parent, struct o2net_msg_handler, nh_node);
  658. cmp = o2net_handler_cmp(nmh, msg_type, key);
  659. if (cmp < 0)
  660. p = &(*p)->rb_left;
  661. else if (cmp > 0)
  662. p = &(*p)->rb_right;
  663. else {
  664. ret = nmh;
  665. break;
  666. }
  667. }
  668. if (ret_p != NULL)
  669. *ret_p = p;
  670. if (ret_parent != NULL)
  671. *ret_parent = parent;
  672. return ret;
  673. }
  674. static void o2net_handler_kref_release(struct kref *kref)
  675. {
  676. struct o2net_msg_handler *nmh;
  677. nmh = container_of(kref, struct o2net_msg_handler, nh_kref);
  678. kfree(nmh);
  679. }
  680. static void o2net_handler_put(struct o2net_msg_handler *nmh)
  681. {
  682. kref_put(&nmh->nh_kref, o2net_handler_kref_release);
  683. }
  684. /* max_len is protection for the handler func. incoming messages won't
  685. * be given to the handler if their payload is longer than the max. */
  686. int o2net_register_handler(u32 msg_type, u32 key, u32 max_len,
  687. o2net_msg_handler_func *func, void *data,
  688. o2net_post_msg_handler_func *post_func,
  689. struct list_head *unreg_list)
  690. {
  691. struct o2net_msg_handler *nmh = NULL;
  692. struct rb_node **p, *parent;
  693. int ret = 0;
  694. if (max_len > O2NET_MAX_PAYLOAD_BYTES) {
  695. mlog(0, "max_len for message handler out of range: %u\n",
  696. max_len);
  697. ret = -EINVAL;
  698. goto out;
  699. }
  700. if (!msg_type) {
  701. mlog(0, "no message type provided: %u, %p\n", msg_type, func);
  702. ret = -EINVAL;
  703. goto out;
  704. }
  705. if (!func) {
  706. mlog(0, "no message handler provided: %u, %p\n",
  707. msg_type, func);
  708. ret = -EINVAL;
  709. goto out;
  710. }
  711. nmh = kzalloc(sizeof(struct o2net_msg_handler), GFP_NOFS);
  712. if (nmh == NULL) {
  713. ret = -ENOMEM;
  714. goto out;
  715. }
  716. nmh->nh_func = func;
  717. nmh->nh_func_data = data;
  718. nmh->nh_post_func = post_func;
  719. nmh->nh_msg_type = msg_type;
  720. nmh->nh_max_len = max_len;
  721. nmh->nh_key = key;
  722. /* the tree and list get this ref.. they're both removed in
  723. * unregister when this ref is dropped */
  724. kref_init(&nmh->nh_kref);
  725. INIT_LIST_HEAD(&nmh->nh_unregister_item);
  726. write_lock(&o2net_handler_lock);
  727. if (o2net_handler_tree_lookup(msg_type, key, &p, &parent))
  728. ret = -EEXIST;
  729. else {
  730. rb_link_node(&nmh->nh_node, parent, p);
  731. rb_insert_color(&nmh->nh_node, &o2net_handler_tree);
  732. list_add_tail(&nmh->nh_unregister_item, unreg_list);
  733. mlog(ML_TCP, "registered handler func %p type %u key %08x\n",
  734. func, msg_type, key);
  735. /* we've had some trouble with handlers seemingly vanishing. */
  736. mlog_bug_on_msg(o2net_handler_tree_lookup(msg_type, key, &p,
  737. &parent) == NULL,
  738. "couldn't find handler we *just* registered "
  739. "for type %u key %08x\n", msg_type, key);
  740. }
  741. write_unlock(&o2net_handler_lock);
  742. if (ret)
  743. goto out;
  744. out:
  745. if (ret)
  746. kfree(nmh);
  747. return ret;
  748. }
  749. EXPORT_SYMBOL_GPL(o2net_register_handler);
  750. void o2net_unregister_handler_list(struct list_head *list)
  751. {
  752. struct o2net_msg_handler *nmh, *n;
  753. write_lock(&o2net_handler_lock);
  754. list_for_each_entry_safe(nmh, n, list, nh_unregister_item) {
  755. mlog(ML_TCP, "unregistering handler func %p type %u key %08x\n",
  756. nmh->nh_func, nmh->nh_msg_type, nmh->nh_key);
  757. rb_erase(&nmh->nh_node, &o2net_handler_tree);
  758. list_del_init(&nmh->nh_unregister_item);
  759. kref_put(&nmh->nh_kref, o2net_handler_kref_release);
  760. }
  761. write_unlock(&o2net_handler_lock);
  762. }
  763. EXPORT_SYMBOL_GPL(o2net_unregister_handler_list);
  764. static struct o2net_msg_handler *o2net_handler_get(u32 msg_type, u32 key)
  765. {
  766. struct o2net_msg_handler *nmh;
  767. read_lock(&o2net_handler_lock);
  768. nmh = o2net_handler_tree_lookup(msg_type, key, NULL, NULL);
  769. if (nmh)
  770. kref_get(&nmh->nh_kref);
  771. read_unlock(&o2net_handler_lock);
  772. return nmh;
  773. }
  774. /* ------------------------------------------------------------ */
  775. static int o2net_recv_tcp_msg(struct socket *sock, void *data, size_t len)
  776. {
  777. struct kvec vec = { .iov_len = len, .iov_base = data, };
  778. struct msghdr msg = { .msg_flags = MSG_DONTWAIT, };
  779. return kernel_recvmsg(sock, &msg, &vec, 1, len, msg.msg_flags);
  780. }
  781. static int o2net_send_tcp_msg(struct socket *sock, struct kvec *vec,
  782. size_t veclen, size_t total)
  783. {
  784. int ret;
  785. struct msghdr msg = {.msg_flags = 0,};
  786. if (sock == NULL) {
  787. ret = -EINVAL;
  788. goto out;
  789. }
  790. ret = kernel_sendmsg(sock, &msg, vec, veclen, total);
  791. if (likely(ret == total))
  792. return 0;
  793. mlog(ML_ERROR, "sendmsg returned %d instead of %zu\n", ret, total);
  794. if (ret >= 0)
  795. ret = -EPIPE; /* should be smarter, I bet */
  796. out:
  797. mlog(0, "returning error: %d\n", ret);
  798. return ret;
  799. }
  800. static void o2net_sendpage(struct o2net_sock_container *sc,
  801. void *kmalloced_virt,
  802. size_t size)
  803. {
  804. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  805. ssize_t ret;
  806. while (1) {
  807. mutex_lock(&sc->sc_send_lock);
  808. ret = sc->sc_sock->ops->sendpage(sc->sc_sock,
  809. virt_to_page(kmalloced_virt),
  810. (long)kmalloced_virt & ~PAGE_MASK,
  811. size, MSG_DONTWAIT);
  812. mutex_unlock(&sc->sc_send_lock);
  813. if (ret == size)
  814. break;
  815. if (ret == (ssize_t)-EAGAIN) {
  816. mlog(0, "sendpage of size %zu to " SC_NODEF_FMT
  817. " returned EAGAIN\n", size, SC_NODEF_ARGS(sc));
  818. cond_resched();
  819. continue;
  820. }
  821. mlog(ML_ERROR, "sendpage of size %zu to " SC_NODEF_FMT
  822. " failed with %zd\n", size, SC_NODEF_ARGS(sc), ret);
  823. o2net_ensure_shutdown(nn, sc, 0);
  824. break;
  825. }
  826. }
  827. static void o2net_init_msg(struct o2net_msg *msg, u16 data_len, u16 msg_type, u32 key)
  828. {
  829. memset(msg, 0, sizeof(struct o2net_msg));
  830. msg->magic = cpu_to_be16(O2NET_MSG_MAGIC);
  831. msg->data_len = cpu_to_be16(data_len);
  832. msg->msg_type = cpu_to_be16(msg_type);
  833. msg->sys_status = cpu_to_be32(O2NET_ERR_NONE);
  834. msg->status = 0;
  835. msg->key = cpu_to_be32(key);
  836. }
  837. static int o2net_tx_can_proceed(struct o2net_node *nn,
  838. struct o2net_sock_container **sc_ret,
  839. int *error)
  840. {
  841. int ret = 0;
  842. spin_lock(&nn->nn_lock);
  843. if (nn->nn_persistent_error) {
  844. ret = 1;
  845. *sc_ret = NULL;
  846. *error = nn->nn_persistent_error;
  847. } else if (nn->nn_sc_valid) {
  848. kref_get(&nn->nn_sc->sc_kref);
  849. ret = 1;
  850. *sc_ret = nn->nn_sc;
  851. *error = 0;
  852. }
  853. spin_unlock(&nn->nn_lock);
  854. return ret;
  855. }
  856. /* Get a map of all nodes to which this node is currently connected to */
  857. void o2net_fill_node_map(unsigned long *map, unsigned bytes)
  858. {
  859. struct o2net_sock_container *sc;
  860. int node, ret;
  861. BUG_ON(bytes < (BITS_TO_LONGS(O2NM_MAX_NODES) * sizeof(unsigned long)));
  862. memset(map, 0, bytes);
  863. for (node = 0; node < O2NM_MAX_NODES; ++node) {
  864. if (!o2net_tx_can_proceed(o2net_nn_from_num(node), &sc, &ret))
  865. continue;
  866. if (!ret) {
  867. set_bit(node, map);
  868. sc_put(sc);
  869. }
  870. }
  871. }
  872. EXPORT_SYMBOL_GPL(o2net_fill_node_map);
  873. int o2net_send_message_vec(u32 msg_type, u32 key, struct kvec *caller_vec,
  874. size_t caller_veclen, u8 target_node, int *status)
  875. {
  876. int ret = 0;
  877. struct o2net_msg *msg = NULL;
  878. size_t veclen, caller_bytes = 0;
  879. struct kvec *vec = NULL;
  880. struct o2net_sock_container *sc = NULL;
  881. struct o2net_node *nn = o2net_nn_from_num(target_node);
  882. struct o2net_status_wait nsw = {
  883. .ns_node_item = LIST_HEAD_INIT(nsw.ns_node_item),
  884. };
  885. struct o2net_send_tracking nst;
  886. o2net_init_nst(&nst, msg_type, key, current, target_node);
  887. if (o2net_wq == NULL) {
  888. mlog(0, "attempt to tx without o2netd running\n");
  889. ret = -ESRCH;
  890. goto out;
  891. }
  892. if (caller_veclen == 0) {
  893. mlog(0, "bad kvec array length\n");
  894. ret = -EINVAL;
  895. goto out;
  896. }
  897. caller_bytes = iov_length((struct iovec *)caller_vec, caller_veclen);
  898. if (caller_bytes > O2NET_MAX_PAYLOAD_BYTES) {
  899. mlog(0, "total payload len %zu too large\n", caller_bytes);
  900. ret = -EINVAL;
  901. goto out;
  902. }
  903. if (target_node == o2nm_this_node()) {
  904. ret = -ELOOP;
  905. goto out;
  906. }
  907. o2net_debug_add_nst(&nst);
  908. o2net_set_nst_sock_time(&nst);
  909. wait_event(nn->nn_sc_wq, o2net_tx_can_proceed(nn, &sc, &ret));
  910. if (ret)
  911. goto out;
  912. o2net_set_nst_sock_container(&nst, sc);
  913. veclen = caller_veclen + 1;
  914. vec = kmalloc(sizeof(struct kvec) * veclen, GFP_ATOMIC);
  915. if (vec == NULL) {
  916. mlog(0, "failed to %zu element kvec!\n", veclen);
  917. ret = -ENOMEM;
  918. goto out;
  919. }
  920. msg = kmalloc(sizeof(struct o2net_msg), GFP_ATOMIC);
  921. if (!msg) {
  922. mlog(0, "failed to allocate a o2net_msg!\n");
  923. ret = -ENOMEM;
  924. goto out;
  925. }
  926. o2net_init_msg(msg, caller_bytes, msg_type, key);
  927. vec[0].iov_len = sizeof(struct o2net_msg);
  928. vec[0].iov_base = msg;
  929. memcpy(&vec[1], caller_vec, caller_veclen * sizeof(struct kvec));
  930. ret = o2net_prep_nsw(nn, &nsw);
  931. if (ret)
  932. goto out;
  933. msg->msg_num = cpu_to_be32(nsw.ns_id);
  934. o2net_set_nst_msg_id(&nst, nsw.ns_id);
  935. o2net_set_nst_send_time(&nst);
  936. /* finally, convert the message header to network byte-order
  937. * and send */
  938. mutex_lock(&sc->sc_send_lock);
  939. ret = o2net_send_tcp_msg(sc->sc_sock, vec, veclen,
  940. sizeof(struct o2net_msg) + caller_bytes);
  941. mutex_unlock(&sc->sc_send_lock);
  942. msglog(msg, "sending returned %d\n", ret);
  943. if (ret < 0) {
  944. mlog(0, "error returned from o2net_send_tcp_msg=%d\n", ret);
  945. goto out;
  946. }
  947. /* wait on other node's handler */
  948. o2net_set_nst_status_time(&nst);
  949. wait_event(nsw.ns_wq, o2net_nsw_completed(nn, &nsw));
  950. o2net_update_send_stats(&nst, sc);
  951. /* Note that we avoid overwriting the callers status return
  952. * variable if a system error was reported on the other
  953. * side. Callers beware. */
  954. ret = o2net_sys_err_to_errno(nsw.ns_sys_status);
  955. if (status && !ret)
  956. *status = nsw.ns_status;
  957. mlog(0, "woken, returning system status %d, user status %d\n",
  958. ret, nsw.ns_status);
  959. out:
  960. o2net_debug_del_nst(&nst); /* must be before dropping sc and node */
  961. if (sc)
  962. sc_put(sc);
  963. kfree(vec);
  964. kfree(msg);
  965. o2net_complete_nsw(nn, &nsw, 0, 0, 0);
  966. return ret;
  967. }
  968. EXPORT_SYMBOL_GPL(o2net_send_message_vec);
  969. int o2net_send_message(u32 msg_type, u32 key, void *data, u32 len,
  970. u8 target_node, int *status)
  971. {
  972. struct kvec vec = {
  973. .iov_base = data,
  974. .iov_len = len,
  975. };
  976. return o2net_send_message_vec(msg_type, key, &vec, 1,
  977. target_node, status);
  978. }
  979. EXPORT_SYMBOL_GPL(o2net_send_message);
  980. static int o2net_send_status_magic(struct socket *sock, struct o2net_msg *hdr,
  981. enum o2net_system_error syserr, int err)
  982. {
  983. struct kvec vec = {
  984. .iov_base = hdr,
  985. .iov_len = sizeof(struct o2net_msg),
  986. };
  987. BUG_ON(syserr >= O2NET_ERR_MAX);
  988. /* leave other fields intact from the incoming message, msg_num
  989. * in particular */
  990. hdr->sys_status = cpu_to_be32(syserr);
  991. hdr->status = cpu_to_be32(err);
  992. hdr->magic = cpu_to_be16(O2NET_MSG_STATUS_MAGIC); // twiddle the magic
  993. hdr->data_len = 0;
  994. msglog(hdr, "about to send status magic %d\n", err);
  995. /* hdr has been in host byteorder this whole time */
  996. return o2net_send_tcp_msg(sock, &vec, 1, sizeof(struct o2net_msg));
  997. }
  998. /* this returns -errno if the header was unknown or too large, etc.
  999. * after this is called the buffer us reused for the next message */
  1000. static int o2net_process_message(struct o2net_sock_container *sc,
  1001. struct o2net_msg *hdr)
  1002. {
  1003. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  1004. int ret = 0, handler_status;
  1005. enum o2net_system_error syserr;
  1006. struct o2net_msg_handler *nmh = NULL;
  1007. void *ret_data = NULL;
  1008. msglog(hdr, "processing message\n");
  1009. o2net_sc_postpone_idle(sc);
  1010. switch(be16_to_cpu(hdr->magic)) {
  1011. case O2NET_MSG_STATUS_MAGIC:
  1012. /* special type for returning message status */
  1013. o2net_complete_nsw(nn, NULL,
  1014. be32_to_cpu(hdr->msg_num),
  1015. be32_to_cpu(hdr->sys_status),
  1016. be32_to_cpu(hdr->status));
  1017. goto out;
  1018. case O2NET_MSG_KEEP_REQ_MAGIC:
  1019. o2net_sendpage(sc, o2net_keep_resp,
  1020. sizeof(*o2net_keep_resp));
  1021. goto out;
  1022. case O2NET_MSG_KEEP_RESP_MAGIC:
  1023. goto out;
  1024. case O2NET_MSG_MAGIC:
  1025. break;
  1026. default:
  1027. msglog(hdr, "bad magic\n");
  1028. ret = -EINVAL;
  1029. goto out;
  1030. break;
  1031. }
  1032. /* find a handler for it */
  1033. handler_status = 0;
  1034. nmh = o2net_handler_get(be16_to_cpu(hdr->msg_type),
  1035. be32_to_cpu(hdr->key));
  1036. if (!nmh) {
  1037. mlog(ML_TCP, "couldn't find handler for type %u key %08x\n",
  1038. be16_to_cpu(hdr->msg_type), be32_to_cpu(hdr->key));
  1039. syserr = O2NET_ERR_NO_HNDLR;
  1040. goto out_respond;
  1041. }
  1042. syserr = O2NET_ERR_NONE;
  1043. if (be16_to_cpu(hdr->data_len) > nmh->nh_max_len)
  1044. syserr = O2NET_ERR_OVERFLOW;
  1045. if (syserr != O2NET_ERR_NONE)
  1046. goto out_respond;
  1047. o2net_set_func_start_time(sc);
  1048. sc->sc_msg_key = be32_to_cpu(hdr->key);
  1049. sc->sc_msg_type = be16_to_cpu(hdr->msg_type);
  1050. handler_status = (nmh->nh_func)(hdr, sizeof(struct o2net_msg) +
  1051. be16_to_cpu(hdr->data_len),
  1052. nmh->nh_func_data, &ret_data);
  1053. o2net_set_func_stop_time(sc);
  1054. o2net_update_recv_stats(sc);
  1055. out_respond:
  1056. /* this destroys the hdr, so don't use it after this */
  1057. mutex_lock(&sc->sc_send_lock);
  1058. ret = o2net_send_status_magic(sc->sc_sock, hdr, syserr,
  1059. handler_status);
  1060. mutex_unlock(&sc->sc_send_lock);
  1061. hdr = NULL;
  1062. mlog(0, "sending handler status %d, syserr %d returned %d\n",
  1063. handler_status, syserr, ret);
  1064. if (nmh) {
  1065. BUG_ON(ret_data != NULL && nmh->nh_post_func == NULL);
  1066. if (nmh->nh_post_func)
  1067. (nmh->nh_post_func)(handler_status, nmh->nh_func_data,
  1068. ret_data);
  1069. }
  1070. out:
  1071. if (nmh)
  1072. o2net_handler_put(nmh);
  1073. return ret;
  1074. }
  1075. static int o2net_check_handshake(struct o2net_sock_container *sc)
  1076. {
  1077. struct o2net_handshake *hand = page_address(sc->sc_page);
  1078. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  1079. if (hand->protocol_version != cpu_to_be64(O2NET_PROTOCOL_VERSION)) {
  1080. printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " Advertised net "
  1081. "protocol version %llu but %llu is required. "
  1082. "Disconnecting.\n", SC_NODEF_ARGS(sc),
  1083. (unsigned long long)be64_to_cpu(hand->protocol_version),
  1084. O2NET_PROTOCOL_VERSION);
  1085. /* don't bother reconnecting if its the wrong version. */
  1086. o2net_ensure_shutdown(nn, sc, -ENOTCONN);
  1087. return -1;
  1088. }
  1089. /*
  1090. * Ensure timeouts are consistent with other nodes, otherwise
  1091. * we can end up with one node thinking that the other must be down,
  1092. * but isn't. This can ultimately cause corruption.
  1093. */
  1094. if (be32_to_cpu(hand->o2net_idle_timeout_ms) !=
  1095. o2net_idle_timeout()) {
  1096. printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " uses a network "
  1097. "idle timeout of %u ms, but we use %u ms locally. "
  1098. "Disconnecting.\n", SC_NODEF_ARGS(sc),
  1099. be32_to_cpu(hand->o2net_idle_timeout_ms),
  1100. o2net_idle_timeout());
  1101. o2net_ensure_shutdown(nn, sc, -ENOTCONN);
  1102. return -1;
  1103. }
  1104. if (be32_to_cpu(hand->o2net_keepalive_delay_ms) !=
  1105. o2net_keepalive_delay()) {
  1106. printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " uses a keepalive "
  1107. "delay of %u ms, but we use %u ms locally. "
  1108. "Disconnecting.\n", SC_NODEF_ARGS(sc),
  1109. be32_to_cpu(hand->o2net_keepalive_delay_ms),
  1110. o2net_keepalive_delay());
  1111. o2net_ensure_shutdown(nn, sc, -ENOTCONN);
  1112. return -1;
  1113. }
  1114. if (be32_to_cpu(hand->o2hb_heartbeat_timeout_ms) !=
  1115. O2HB_MAX_WRITE_TIMEOUT_MS) {
  1116. printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " uses a heartbeat "
  1117. "timeout of %u ms, but we use %u ms locally. "
  1118. "Disconnecting.\n", SC_NODEF_ARGS(sc),
  1119. be32_to_cpu(hand->o2hb_heartbeat_timeout_ms),
  1120. O2HB_MAX_WRITE_TIMEOUT_MS);
  1121. o2net_ensure_shutdown(nn, sc, -ENOTCONN);
  1122. return -1;
  1123. }
  1124. sc->sc_handshake_ok = 1;
  1125. spin_lock(&nn->nn_lock);
  1126. /* set valid and queue the idle timers only if it hasn't been
  1127. * shut down already */
  1128. if (nn->nn_sc == sc) {
  1129. o2net_sc_reset_idle_timer(sc);
  1130. atomic_set(&nn->nn_timeout, 0);
  1131. o2net_set_nn_state(nn, sc, 1, 0);
  1132. }
  1133. spin_unlock(&nn->nn_lock);
  1134. /* shift everything up as though it wasn't there */
  1135. sc->sc_page_off -= sizeof(struct o2net_handshake);
  1136. if (sc->sc_page_off)
  1137. memmove(hand, hand + 1, sc->sc_page_off);
  1138. return 0;
  1139. }
  1140. /* this demuxes the queued rx bytes into header or payload bits and calls
  1141. * handlers as each full message is read off the socket. it returns -error,
  1142. * == 0 eof, or > 0 for progress made.*/
  1143. static int o2net_advance_rx(struct o2net_sock_container *sc)
  1144. {
  1145. struct o2net_msg *hdr;
  1146. int ret = 0;
  1147. void *data;
  1148. size_t datalen;
  1149. sclog(sc, "receiving\n");
  1150. o2net_set_advance_start_time(sc);
  1151. if (unlikely(sc->sc_handshake_ok == 0)) {
  1152. if(sc->sc_page_off < sizeof(struct o2net_handshake)) {
  1153. data = page_address(sc->sc_page) + sc->sc_page_off;
  1154. datalen = sizeof(struct o2net_handshake) - sc->sc_page_off;
  1155. ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
  1156. if (ret > 0)
  1157. sc->sc_page_off += ret;
  1158. }
  1159. if (sc->sc_page_off == sizeof(struct o2net_handshake)) {
  1160. o2net_check_handshake(sc);
  1161. if (unlikely(sc->sc_handshake_ok == 0))
  1162. ret = -EPROTO;
  1163. }
  1164. goto out;
  1165. }
  1166. /* do we need more header? */
  1167. if (sc->sc_page_off < sizeof(struct o2net_msg)) {
  1168. data = page_address(sc->sc_page) + sc->sc_page_off;
  1169. datalen = sizeof(struct o2net_msg) - sc->sc_page_off;
  1170. ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
  1171. if (ret > 0) {
  1172. sc->sc_page_off += ret;
  1173. /* only swab incoming here.. we can
  1174. * only get here once as we cross from
  1175. * being under to over */
  1176. if (sc->sc_page_off == sizeof(struct o2net_msg)) {
  1177. hdr = page_address(sc->sc_page);
  1178. if (be16_to_cpu(hdr->data_len) >
  1179. O2NET_MAX_PAYLOAD_BYTES)
  1180. ret = -EOVERFLOW;
  1181. }
  1182. }
  1183. if (ret <= 0)
  1184. goto out;
  1185. }
  1186. if (sc->sc_page_off < sizeof(struct o2net_msg)) {
  1187. /* oof, still don't have a header */
  1188. goto out;
  1189. }
  1190. /* this was swabbed above when we first read it */
  1191. hdr = page_address(sc->sc_page);
  1192. msglog(hdr, "at page_off %zu\n", sc->sc_page_off);
  1193. /* do we need more payload? */
  1194. if (sc->sc_page_off - sizeof(struct o2net_msg) < be16_to_cpu(hdr->data_len)) {
  1195. /* need more payload */
  1196. data = page_address(sc->sc_page) + sc->sc_page_off;
  1197. datalen = (sizeof(struct o2net_msg) + be16_to_cpu(hdr->data_len)) -
  1198. sc->sc_page_off;
  1199. ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
  1200. if (ret > 0)
  1201. sc->sc_page_off += ret;
  1202. if (ret <= 0)
  1203. goto out;
  1204. }
  1205. if (sc->sc_page_off - sizeof(struct o2net_msg) == be16_to_cpu(hdr->data_len)) {
  1206. /* we can only get here once, the first time we read
  1207. * the payload.. so set ret to progress if the handler
  1208. * works out. after calling this the message is toast */
  1209. ret = o2net_process_message(sc, hdr);
  1210. if (ret == 0)
  1211. ret = 1;
  1212. sc->sc_page_off = 0;
  1213. }
  1214. out:
  1215. sclog(sc, "ret = %d\n", ret);
  1216. o2net_set_advance_stop_time(sc);
  1217. return ret;
  1218. }
  1219. /* this work func is triggerd by data ready. it reads until it can read no
  1220. * more. it interprets 0, eof, as fatal. if data_ready hits while we're doing
  1221. * our work the work struct will be marked and we'll be called again. */
  1222. static void o2net_rx_until_empty(struct work_struct *work)
  1223. {
  1224. struct o2net_sock_container *sc =
  1225. container_of(work, struct o2net_sock_container, sc_rx_work);
  1226. int ret;
  1227. do {
  1228. ret = o2net_advance_rx(sc);
  1229. } while (ret > 0);
  1230. if (ret <= 0 && ret != -EAGAIN) {
  1231. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  1232. sclog(sc, "saw error %d, closing\n", ret);
  1233. /* not permanent so read failed handshake can retry */
  1234. o2net_ensure_shutdown(nn, sc, 0);
  1235. }
  1236. sc_put(sc);
  1237. }
  1238. static int o2net_set_nodelay(struct socket *sock)
  1239. {
  1240. int ret, val = 1;
  1241. mm_segment_t oldfs;
  1242. oldfs = get_fs();
  1243. set_fs(KERNEL_DS);
  1244. /*
  1245. * Dear unsuspecting programmer,
  1246. *
  1247. * Don't use sock_setsockopt() for SOL_TCP. It doesn't check its level
  1248. * argument and assumes SOL_SOCKET so, say, your TCP_NODELAY will
  1249. * silently turn into SO_DEBUG.
  1250. *
  1251. * Yours,
  1252. * Keeper of hilariously fragile interfaces.
  1253. */
  1254. ret = sock->ops->setsockopt(sock, SOL_TCP, TCP_NODELAY,
  1255. (char __user *)&val, sizeof(val));
  1256. set_fs(oldfs);
  1257. return ret;
  1258. }
  1259. static int o2net_set_usertimeout(struct socket *sock)
  1260. {
  1261. int user_timeout = O2NET_TCP_USER_TIMEOUT;
  1262. return kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
  1263. (char *)&user_timeout, sizeof(user_timeout));
  1264. }
  1265. static void o2net_initialize_handshake(void)
  1266. {
  1267. o2net_hand->o2hb_heartbeat_timeout_ms = cpu_to_be32(
  1268. O2HB_MAX_WRITE_TIMEOUT_MS);
  1269. o2net_hand->o2net_idle_timeout_ms = cpu_to_be32(o2net_idle_timeout());
  1270. o2net_hand->o2net_keepalive_delay_ms = cpu_to_be32(
  1271. o2net_keepalive_delay());
  1272. o2net_hand->o2net_reconnect_delay_ms = cpu_to_be32(
  1273. o2net_reconnect_delay());
  1274. }
  1275. /* ------------------------------------------------------------ */
  1276. /* called when a connect completes and after a sock is accepted. the
  1277. * rx path will see the response and mark the sc valid */
  1278. static void o2net_sc_connect_completed(struct work_struct *work)
  1279. {
  1280. struct o2net_sock_container *sc =
  1281. container_of(work, struct o2net_sock_container,
  1282. sc_connect_work);
  1283. mlog(ML_MSG, "sc sending handshake with ver %llu id %llx\n",
  1284. (unsigned long long)O2NET_PROTOCOL_VERSION,
  1285. (unsigned long long)be64_to_cpu(o2net_hand->connector_id));
  1286. o2net_initialize_handshake();
  1287. o2net_sendpage(sc, o2net_hand, sizeof(*o2net_hand));
  1288. sc_put(sc);
  1289. }
  1290. /* this is called as a work_struct func. */
  1291. static void o2net_sc_send_keep_req(struct work_struct *work)
  1292. {
  1293. struct o2net_sock_container *sc =
  1294. container_of(work, struct o2net_sock_container,
  1295. sc_keepalive_work.work);
  1296. o2net_sendpage(sc, o2net_keep_req, sizeof(*o2net_keep_req));
  1297. sc_put(sc);
  1298. }
  1299. /* socket shutdown does a del_timer_sync against this as it tears down.
  1300. * we can't start this timer until we've got to the point in sc buildup
  1301. * where shutdown is going to be involved */
  1302. static void o2net_idle_timer(unsigned long data)
  1303. {
  1304. struct o2net_sock_container *sc = (struct o2net_sock_container *)data;
  1305. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  1306. #ifdef CONFIG_DEBUG_FS
  1307. unsigned long msecs = ktime_to_ms(ktime_get()) -
  1308. ktime_to_ms(sc->sc_tv_timer);
  1309. #else
  1310. unsigned long msecs = o2net_idle_timeout();
  1311. #endif
  1312. printk(KERN_NOTICE "o2net: Connection to " SC_NODEF_FMT " has been "
  1313. "idle for %lu.%lu secs.\n",
  1314. SC_NODEF_ARGS(sc), msecs / 1000, msecs % 1000);
  1315. /* idle timerout happen, don't shutdown the connection, but
  1316. * make fence decision. Maybe the connection can recover before
  1317. * the decision is made.
  1318. */
  1319. atomic_set(&nn->nn_timeout, 1);
  1320. o2quo_conn_err(o2net_num_from_nn(nn));
  1321. queue_delayed_work(o2net_wq, &nn->nn_still_up,
  1322. msecs_to_jiffies(O2NET_QUORUM_DELAY_MS));
  1323. o2net_sc_reset_idle_timer(sc);
  1324. }
  1325. static void o2net_sc_reset_idle_timer(struct o2net_sock_container *sc)
  1326. {
  1327. o2net_sc_cancel_delayed_work(sc, &sc->sc_keepalive_work);
  1328. o2net_sc_queue_delayed_work(sc, &sc->sc_keepalive_work,
  1329. msecs_to_jiffies(o2net_keepalive_delay()));
  1330. o2net_set_sock_timer(sc);
  1331. mod_timer(&sc->sc_idle_timeout,
  1332. jiffies + msecs_to_jiffies(o2net_idle_timeout()));
  1333. }
  1334. static void o2net_sc_postpone_idle(struct o2net_sock_container *sc)
  1335. {
  1336. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  1337. /* clear fence decision since the connection recover from timeout*/
  1338. if (atomic_read(&nn->nn_timeout)) {
  1339. o2quo_conn_up(o2net_num_from_nn(nn));
  1340. cancel_delayed_work(&nn->nn_still_up);
  1341. atomic_set(&nn->nn_timeout, 0);
  1342. }
  1343. /* Only push out an existing timer */
  1344. if (timer_pending(&sc->sc_idle_timeout))
  1345. o2net_sc_reset_idle_timer(sc);
  1346. }
  1347. /* this work func is kicked whenever a path sets the nn state which doesn't
  1348. * have valid set. This includes seeing hb come up, losing a connection,
  1349. * having a connect attempt fail, etc. This centralizes the logic which decides
  1350. * if a connect attempt should be made or if we should give up and all future
  1351. * transmit attempts should fail */
  1352. static void o2net_start_connect(struct work_struct *work)
  1353. {
  1354. struct o2net_node *nn =
  1355. container_of(work, struct o2net_node, nn_connect_work.work);
  1356. struct o2net_sock_container *sc = NULL;
  1357. struct o2nm_node *node = NULL, *mynode = NULL;
  1358. struct socket *sock = NULL;
  1359. struct sockaddr_in myaddr = {0, }, remoteaddr = {0, };
  1360. int ret = 0, stop;
  1361. unsigned int timeout;
  1362. unsigned int noio_flag;
  1363. /*
  1364. * sock_create allocates the sock with GFP_KERNEL. We must set
  1365. * per-process flag PF_MEMALLOC_NOIO so that all allocations done
  1366. * by this process are done as if GFP_NOIO was specified. So we
  1367. * are not reentering filesystem while doing memory reclaim.
  1368. */
  1369. noio_flag = memalloc_noio_save();
  1370. /* if we're greater we initiate tx, otherwise we accept */
  1371. if (o2nm_this_node() <= o2net_num_from_nn(nn))
  1372. goto out;
  1373. /* watch for racing with tearing a node down */
  1374. node = o2nm_get_node_by_num(o2net_num_from_nn(nn));
  1375. if (node == NULL) {
  1376. ret = 0;
  1377. goto out;
  1378. }
  1379. mynode = o2nm_get_node_by_num(o2nm_this_node());
  1380. if (mynode == NULL) {
  1381. ret = 0;
  1382. goto out;
  1383. }
  1384. spin_lock(&nn->nn_lock);
  1385. /*
  1386. * see if we already have one pending or have given up.
  1387. * For nn_timeout, it is set when we close the connection
  1388. * because of the idle time out. So it means that we have
  1389. * at least connected to that node successfully once,
  1390. * now try to connect to it again.
  1391. */
  1392. timeout = atomic_read(&nn->nn_timeout);
  1393. stop = (nn->nn_sc ||
  1394. (nn->nn_persistent_error &&
  1395. (nn->nn_persistent_error != -ENOTCONN || timeout == 0)));
  1396. spin_unlock(&nn->nn_lock);
  1397. if (stop)
  1398. goto out;
  1399. nn->nn_last_connect_attempt = jiffies;
  1400. sc = sc_alloc(node);
  1401. if (sc == NULL) {
  1402. mlog(0, "couldn't allocate sc\n");
  1403. ret = -ENOMEM;
  1404. goto out;
  1405. }
  1406. ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
  1407. if (ret < 0) {
  1408. mlog(0, "can't create socket: %d\n", ret);
  1409. goto out;
  1410. }
  1411. sc->sc_sock = sock; /* freed by sc_kref_release */
  1412. sock->sk->sk_allocation = GFP_ATOMIC;
  1413. myaddr.sin_family = AF_INET;
  1414. myaddr.sin_addr.s_addr = mynode->nd_ipv4_address;
  1415. myaddr.sin_port = htons(0); /* any port */
  1416. ret = sock->ops->bind(sock, (struct sockaddr *)&myaddr,
  1417. sizeof(myaddr));
  1418. if (ret) {
  1419. mlog(ML_ERROR, "bind failed with %d at address %pI4\n",
  1420. ret, &mynode->nd_ipv4_address);
  1421. goto out;
  1422. }
  1423. ret = o2net_set_nodelay(sc->sc_sock);
  1424. if (ret) {
  1425. mlog(ML_ERROR, "setting TCP_NODELAY failed with %d\n", ret);
  1426. goto out;
  1427. }
  1428. ret = o2net_set_usertimeout(sock);
  1429. if (ret) {
  1430. mlog(ML_ERROR, "set TCP_USER_TIMEOUT failed with %d\n", ret);
  1431. goto out;
  1432. }
  1433. o2net_register_callbacks(sc->sc_sock->sk, sc);
  1434. spin_lock(&nn->nn_lock);
  1435. /* handshake completion will set nn->nn_sc_valid */
  1436. o2net_set_nn_state(nn, sc, 0, 0);
  1437. spin_unlock(&nn->nn_lock);
  1438. remoteaddr.sin_family = AF_INET;
  1439. remoteaddr.sin_addr.s_addr = node->nd_ipv4_address;
  1440. remoteaddr.sin_port = node->nd_ipv4_port;
  1441. ret = sc->sc_sock->ops->connect(sc->sc_sock,
  1442. (struct sockaddr *)&remoteaddr,
  1443. sizeof(remoteaddr),
  1444. O_NONBLOCK);
  1445. if (ret == -EINPROGRESS)
  1446. ret = 0;
  1447. out:
  1448. if (ret && sc) {
  1449. printk(KERN_NOTICE "o2net: Connect attempt to " SC_NODEF_FMT
  1450. " failed with errno %d\n", SC_NODEF_ARGS(sc), ret);
  1451. /* 0 err so that another will be queued and attempted
  1452. * from set_nn_state */
  1453. o2net_ensure_shutdown(nn, sc, 0);
  1454. }
  1455. if (sc)
  1456. sc_put(sc);
  1457. if (node)
  1458. o2nm_node_put(node);
  1459. if (mynode)
  1460. o2nm_node_put(mynode);
  1461. memalloc_noio_restore(noio_flag);
  1462. return;
  1463. }
  1464. static void o2net_connect_expired(struct work_struct *work)
  1465. {
  1466. struct o2net_node *nn =
  1467. container_of(work, struct o2net_node, nn_connect_expired.work);
  1468. spin_lock(&nn->nn_lock);
  1469. if (!nn->nn_sc_valid) {
  1470. printk(KERN_NOTICE "o2net: No connection established with "
  1471. "node %u after %u.%u seconds, check network and"
  1472. " cluster configuration.\n",
  1473. o2net_num_from_nn(nn),
  1474. o2net_idle_timeout() / 1000,
  1475. o2net_idle_timeout() % 1000);
  1476. o2net_set_nn_state(nn, NULL, 0, 0);
  1477. }
  1478. spin_unlock(&nn->nn_lock);
  1479. }
  1480. static void o2net_still_up(struct work_struct *work)
  1481. {
  1482. struct o2net_node *nn =
  1483. container_of(work, struct o2net_node, nn_still_up.work);
  1484. o2quo_hb_still_up(o2net_num_from_nn(nn));
  1485. }
  1486. /* ------------------------------------------------------------ */
  1487. void o2net_disconnect_node(struct o2nm_node *node)
  1488. {
  1489. struct o2net_node *nn = o2net_nn_from_num(node->nd_num);
  1490. /* don't reconnect until it's heartbeating again */
  1491. spin_lock(&nn->nn_lock);
  1492. atomic_set(&nn->nn_timeout, 0);
  1493. o2net_set_nn_state(nn, NULL, 0, -ENOTCONN);
  1494. spin_unlock(&nn->nn_lock);
  1495. if (o2net_wq) {
  1496. cancel_delayed_work(&nn->nn_connect_expired);
  1497. cancel_delayed_work(&nn->nn_connect_work);
  1498. cancel_delayed_work(&nn->nn_still_up);
  1499. flush_workqueue(o2net_wq);
  1500. }
  1501. }
  1502. static void o2net_hb_node_down_cb(struct o2nm_node *node, int node_num,
  1503. void *data)
  1504. {
  1505. o2quo_hb_down(node_num);
  1506. if (!node)
  1507. return;
  1508. if (node_num != o2nm_this_node())
  1509. o2net_disconnect_node(node);
  1510. BUG_ON(atomic_read(&o2net_connected_peers) < 0);
  1511. }
  1512. static void o2net_hb_node_up_cb(struct o2nm_node *node, int node_num,
  1513. void *data)
  1514. {
  1515. struct o2net_node *nn = o2net_nn_from_num(node_num);
  1516. o2quo_hb_up(node_num);
  1517. BUG_ON(!node);
  1518. /* ensure an immediate connect attempt */
  1519. nn->nn_last_connect_attempt = jiffies -
  1520. (msecs_to_jiffies(o2net_reconnect_delay()) + 1);
  1521. if (node_num != o2nm_this_node()) {
  1522. /* believe it or not, accept and node hearbeating testing
  1523. * can succeed for this node before we got here.. so
  1524. * only use set_nn_state to clear the persistent error
  1525. * if that hasn't already happened */
  1526. spin_lock(&nn->nn_lock);
  1527. atomic_set(&nn->nn_timeout, 0);
  1528. if (nn->nn_persistent_error)
  1529. o2net_set_nn_state(nn, NULL, 0, 0);
  1530. spin_unlock(&nn->nn_lock);
  1531. }
  1532. }
  1533. void o2net_unregister_hb_callbacks(void)
  1534. {
  1535. o2hb_unregister_callback(NULL, &o2net_hb_up);
  1536. o2hb_unregister_callback(NULL, &o2net_hb_down);
  1537. }
  1538. int o2net_register_hb_callbacks(void)
  1539. {
  1540. int ret;
  1541. o2hb_setup_callback(&o2net_hb_down, O2HB_NODE_DOWN_CB,
  1542. o2net_hb_node_down_cb, NULL, O2NET_HB_PRI);
  1543. o2hb_setup_callback(&o2net_hb_up, O2HB_NODE_UP_CB,
  1544. o2net_hb_node_up_cb, NULL, O2NET_HB_PRI);
  1545. ret = o2hb_register_callback(NULL, &o2net_hb_up);
  1546. if (ret == 0)
  1547. ret = o2hb_register_callback(NULL, &o2net_hb_down);
  1548. if (ret)
  1549. o2net_unregister_hb_callbacks();
  1550. return ret;
  1551. }
  1552. /* ------------------------------------------------------------ */
  1553. static int o2net_accept_one(struct socket *sock, int *more)
  1554. {
  1555. int ret, slen;
  1556. struct sockaddr_in sin;
  1557. struct socket *new_sock = NULL;
  1558. struct o2nm_node *node = NULL;
  1559. struct o2nm_node *local_node = NULL;
  1560. struct o2net_sock_container *sc = NULL;
  1561. struct o2net_node *nn;
  1562. unsigned int noio_flag;
  1563. /*
  1564. * sock_create_lite allocates the sock with GFP_KERNEL. We must set
  1565. * per-process flag PF_MEMALLOC_NOIO so that all allocations done
  1566. * by this process are done as if GFP_NOIO was specified. So we
  1567. * are not reentering filesystem while doing memory reclaim.
  1568. */
  1569. noio_flag = memalloc_noio_save();
  1570. BUG_ON(sock == NULL);
  1571. *more = 0;
  1572. ret = sock_create_lite(sock->sk->sk_family, sock->sk->sk_type,
  1573. sock->sk->sk_protocol, &new_sock);
  1574. if (ret)
  1575. goto out;
  1576. new_sock->type = sock->type;
  1577. new_sock->ops = sock->ops;
  1578. ret = sock->ops->accept(sock, new_sock, O_NONBLOCK);
  1579. if (ret < 0)
  1580. goto out;
  1581. *more = 1;
  1582. new_sock->sk->sk_allocation = GFP_ATOMIC;
  1583. ret = o2net_set_nodelay(new_sock);
  1584. if (ret) {
  1585. mlog(ML_ERROR, "setting TCP_NODELAY failed with %d\n", ret);
  1586. goto out;
  1587. }
  1588. ret = o2net_set_usertimeout(new_sock);
  1589. if (ret) {
  1590. mlog(ML_ERROR, "set TCP_USER_TIMEOUT failed with %d\n", ret);
  1591. goto out;
  1592. }
  1593. slen = sizeof(sin);
  1594. ret = new_sock->ops->getname(new_sock, (struct sockaddr *) &sin,
  1595. &slen, 1);
  1596. if (ret < 0)
  1597. goto out;
  1598. node = o2nm_get_node_by_ip(sin.sin_addr.s_addr);
  1599. if (node == NULL) {
  1600. printk(KERN_NOTICE "o2net: Attempt to connect from unknown "
  1601. "node at %pI4:%d\n", &sin.sin_addr.s_addr,
  1602. ntohs(sin.sin_port));
  1603. ret = -EINVAL;
  1604. goto out;
  1605. }
  1606. if (o2nm_this_node() >= node->nd_num) {
  1607. local_node = o2nm_get_node_by_num(o2nm_this_node());
  1608. if (local_node)
  1609. printk(KERN_NOTICE "o2net: Unexpected connect attempt "
  1610. "seen at node '%s' (%u, %pI4:%d) from "
  1611. "node '%s' (%u, %pI4:%d)\n",
  1612. local_node->nd_name, local_node->nd_num,
  1613. &(local_node->nd_ipv4_address),
  1614. ntohs(local_node->nd_ipv4_port),
  1615. node->nd_name,
  1616. node->nd_num, &sin.sin_addr.s_addr,
  1617. ntohs(sin.sin_port));
  1618. ret = -EINVAL;
  1619. goto out;
  1620. }
  1621. /* this happens all the time when the other node sees our heartbeat
  1622. * and tries to connect before we see their heartbeat */
  1623. if (!o2hb_check_node_heartbeating_from_callback(node->nd_num)) {
  1624. mlog(ML_CONN, "attempt to connect from node '%s' at "
  1625. "%pI4:%d but it isn't heartbeating\n",
  1626. node->nd_name, &sin.sin_addr.s_addr,
  1627. ntohs(sin.sin_port));
  1628. ret = -EINVAL;
  1629. goto out;
  1630. }
  1631. nn = o2net_nn_from_num(node->nd_num);
  1632. spin_lock(&nn->nn_lock);
  1633. if (nn->nn_sc)
  1634. ret = -EBUSY;
  1635. else
  1636. ret = 0;
  1637. spin_unlock(&nn->nn_lock);
  1638. if (ret) {
  1639. printk(KERN_NOTICE "o2net: Attempt to connect from node '%s' "
  1640. "at %pI4:%d but it already has an open connection\n",
  1641. node->nd_name, &sin.sin_addr.s_addr,
  1642. ntohs(sin.sin_port));
  1643. goto out;
  1644. }
  1645. sc = sc_alloc(node);
  1646. if (sc == NULL) {
  1647. ret = -ENOMEM;
  1648. goto out;
  1649. }
  1650. sc->sc_sock = new_sock;
  1651. new_sock = NULL;
  1652. spin_lock(&nn->nn_lock);
  1653. atomic_set(&nn->nn_timeout, 0);
  1654. o2net_set_nn_state(nn, sc, 0, 0);
  1655. spin_unlock(&nn->nn_lock);
  1656. o2net_register_callbacks(sc->sc_sock->sk, sc);
  1657. o2net_sc_queue_work(sc, &sc->sc_rx_work);
  1658. o2net_initialize_handshake();
  1659. o2net_sendpage(sc, o2net_hand, sizeof(*o2net_hand));
  1660. out:
  1661. if (new_sock)
  1662. sock_release(new_sock);
  1663. if (node)
  1664. o2nm_node_put(node);
  1665. if (local_node)
  1666. o2nm_node_put(local_node);
  1667. if (sc)
  1668. sc_put(sc);
  1669. memalloc_noio_restore(noio_flag);
  1670. return ret;
  1671. }
  1672. /*
  1673. * This function is invoked in response to one or more
  1674. * pending accepts at softIRQ level. We must drain the
  1675. * entire que before returning.
  1676. */
  1677. static void o2net_accept_many(struct work_struct *work)
  1678. {
  1679. struct socket *sock = o2net_listen_sock;
  1680. int more;
  1681. int err;
  1682. /*
  1683. * It is critical to note that due to interrupt moderation
  1684. * at the network driver level, we can't assume to get a
  1685. * softIRQ for every single conn since tcp SYN packets
  1686. * can arrive back-to-back, and therefore many pending
  1687. * accepts may result in just 1 softIRQ. If we terminate
  1688. * the o2net_accept_one() loop upon seeing an err, what happens
  1689. * to the rest of the conns in the queue? If no new SYN
  1690. * arrives for hours, no softIRQ will be delivered,
  1691. * and the connections will just sit in the queue.
  1692. */
  1693. for (;;) {
  1694. err = o2net_accept_one(sock, &more);
  1695. if (!more)
  1696. break;
  1697. cond_resched();
  1698. }
  1699. }
  1700. static void o2net_listen_data_ready(struct sock *sk)
  1701. {
  1702. void (*ready)(struct sock *sk);
  1703. read_lock(&sk->sk_callback_lock);
  1704. ready = sk->sk_user_data;
  1705. if (ready == NULL) { /* check for teardown race */
  1706. ready = sk->sk_data_ready;
  1707. goto out;
  1708. }
  1709. /* This callback may called twice when a new connection
  1710. * is being established as a child socket inherits everything
  1711. * from a parent LISTEN socket, including the data_ready cb of
  1712. * the parent. This leads to a hazard. In o2net_accept_one()
  1713. * we are still initializing the child socket but have not
  1714. * changed the inherited data_ready callback yet when
  1715. * data starts arriving.
  1716. * We avoid this hazard by checking the state.
  1717. * For the listening socket, the state will be TCP_LISTEN; for the new
  1718. * socket, will be TCP_ESTABLISHED. Also, in this case,
  1719. * sk->sk_user_data is not a valid function pointer.
  1720. */
  1721. if (sk->sk_state == TCP_LISTEN) {
  1722. queue_work(o2net_wq, &o2net_listen_work);
  1723. } else {
  1724. ready = NULL;
  1725. }
  1726. out:
  1727. read_unlock(&sk->sk_callback_lock);
  1728. if (ready != NULL)
  1729. ready(sk);
  1730. }
  1731. static int o2net_open_listening_sock(__be32 addr, __be16 port)
  1732. {
  1733. struct socket *sock = NULL;
  1734. int ret;
  1735. struct sockaddr_in sin = {
  1736. .sin_family = PF_INET,
  1737. .sin_addr = { .s_addr = addr },
  1738. .sin_port = port,
  1739. };
  1740. ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
  1741. if (ret < 0) {
  1742. printk(KERN_ERR "o2net: Error %d while creating socket\n", ret);
  1743. goto out;
  1744. }
  1745. sock->sk->sk_allocation = GFP_ATOMIC;
  1746. write_lock_bh(&sock->sk->sk_callback_lock);
  1747. sock->sk->sk_user_data = sock->sk->sk_data_ready;
  1748. sock->sk->sk_data_ready = o2net_listen_data_ready;
  1749. write_unlock_bh(&sock->sk->sk_callback_lock);
  1750. o2net_listen_sock = sock;
  1751. INIT_WORK(&o2net_listen_work, o2net_accept_many);
  1752. sock->sk->sk_reuse = SK_CAN_REUSE;
  1753. ret = sock->ops->bind(sock, (struct sockaddr *)&sin, sizeof(sin));
  1754. if (ret < 0) {
  1755. printk(KERN_ERR "o2net: Error %d while binding socket at "
  1756. "%pI4:%u\n", ret, &addr, ntohs(port));
  1757. goto out;
  1758. }
  1759. ret = sock->ops->listen(sock, 64);
  1760. if (ret < 0)
  1761. printk(KERN_ERR "o2net: Error %d while listening on %pI4:%u\n",
  1762. ret, &addr, ntohs(port));
  1763. out:
  1764. if (ret) {
  1765. o2net_listen_sock = NULL;
  1766. if (sock)
  1767. sock_release(sock);
  1768. }
  1769. return ret;
  1770. }
  1771. /*
  1772. * called from node manager when we should bring up our network listening
  1773. * socket. node manager handles all the serialization to only call this
  1774. * once and to match it with o2net_stop_listening(). note,
  1775. * o2nm_this_node() doesn't work yet as we're being called while it
  1776. * is being set up.
  1777. */
  1778. int o2net_start_listening(struct o2nm_node *node)
  1779. {
  1780. int ret = 0;
  1781. BUG_ON(o2net_wq != NULL);
  1782. BUG_ON(o2net_listen_sock != NULL);
  1783. mlog(ML_KTHREAD, "starting o2net thread...\n");
  1784. o2net_wq = create_singlethread_workqueue("o2net");
  1785. if (o2net_wq == NULL) {
  1786. mlog(ML_ERROR, "unable to launch o2net thread\n");
  1787. return -ENOMEM; /* ? */
  1788. }
  1789. ret = o2net_open_listening_sock(node->nd_ipv4_address,
  1790. node->nd_ipv4_port);
  1791. if (ret) {
  1792. destroy_workqueue(o2net_wq);
  1793. o2net_wq = NULL;
  1794. } else
  1795. o2quo_conn_up(node->nd_num);
  1796. return ret;
  1797. }
  1798. /* again, o2nm_this_node() doesn't work here as we're involved in
  1799. * tearing it down */
  1800. void o2net_stop_listening(struct o2nm_node *node)
  1801. {
  1802. struct socket *sock = o2net_listen_sock;
  1803. size_t i;
  1804. BUG_ON(o2net_wq == NULL);
  1805. BUG_ON(o2net_listen_sock == NULL);
  1806. /* stop the listening socket from generating work */
  1807. write_lock_bh(&sock->sk->sk_callback_lock);
  1808. sock->sk->sk_data_ready = sock->sk->sk_user_data;
  1809. sock->sk->sk_user_data = NULL;
  1810. write_unlock_bh(&sock->sk->sk_callback_lock);
  1811. for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) {
  1812. struct o2nm_node *node = o2nm_get_node_by_num(i);
  1813. if (node) {
  1814. o2net_disconnect_node(node);
  1815. o2nm_node_put(node);
  1816. }
  1817. }
  1818. /* finish all work and tear down the work queue */
  1819. mlog(ML_KTHREAD, "waiting for o2net thread to exit....\n");
  1820. destroy_workqueue(o2net_wq);
  1821. o2net_wq = NULL;
  1822. sock_release(o2net_listen_sock);
  1823. o2net_listen_sock = NULL;
  1824. o2quo_conn_err(node->nd_num);
  1825. }
  1826. /* ------------------------------------------------------------ */
  1827. int o2net_init(void)
  1828. {
  1829. unsigned long i;
  1830. o2quo_init();
  1831. if (o2net_debugfs_init())
  1832. goto out;
  1833. o2net_hand = kzalloc(sizeof(struct o2net_handshake), GFP_KERNEL);
  1834. o2net_keep_req = kzalloc(sizeof(struct o2net_msg), GFP_KERNEL);
  1835. o2net_keep_resp = kzalloc(sizeof(struct o2net_msg), GFP_KERNEL);
  1836. if (!o2net_hand || !o2net_keep_req || !o2net_keep_resp)
  1837. goto out;
  1838. o2net_hand->protocol_version = cpu_to_be64(O2NET_PROTOCOL_VERSION);
  1839. o2net_hand->connector_id = cpu_to_be64(1);
  1840. o2net_keep_req->magic = cpu_to_be16(O2NET_MSG_KEEP_REQ_MAGIC);
  1841. o2net_keep_resp->magic = cpu_to_be16(O2NET_MSG_KEEP_RESP_MAGIC);
  1842. for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) {
  1843. struct o2net_node *nn = o2net_nn_from_num(i);
  1844. atomic_set(&nn->nn_timeout, 0);
  1845. spin_lock_init(&nn->nn_lock);
  1846. INIT_DELAYED_WORK(&nn->nn_connect_work, o2net_start_connect);
  1847. INIT_DELAYED_WORK(&nn->nn_connect_expired,
  1848. o2net_connect_expired);
  1849. INIT_DELAYED_WORK(&nn->nn_still_up, o2net_still_up);
  1850. /* until we see hb from a node we'll return einval */
  1851. nn->nn_persistent_error = -ENOTCONN;
  1852. init_waitqueue_head(&nn->nn_sc_wq);
  1853. idr_init(&nn->nn_status_idr);
  1854. INIT_LIST_HEAD(&nn->nn_status_list);
  1855. }
  1856. return 0;
  1857. out:
  1858. kfree(o2net_hand);
  1859. kfree(o2net_keep_req);
  1860. kfree(o2net_keep_resp);
  1861. o2net_debugfs_exit();
  1862. o2quo_exit();
  1863. return -ENOMEM;
  1864. }
  1865. void o2net_exit(void)
  1866. {
  1867. o2quo_exit();
  1868. kfree(o2net_hand);
  1869. kfree(o2net_keep_req);
  1870. kfree(o2net_keep_resp);
  1871. o2net_debugfs_exit();
  1872. }