tcp.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217
  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. struct o2net_sock_container *sc;
  512. read_lock_bh(&sk->sk_callback_lock);
  513. sc = sk->sk_user_data;
  514. if (sc) {
  515. sclog(sc, "data_ready hit\n");
  516. o2net_set_data_ready_time(sc);
  517. o2net_sc_queue_work(sc, &sc->sc_rx_work);
  518. ready = sc->sc_data_ready;
  519. } else {
  520. ready = sk->sk_data_ready;
  521. }
  522. read_unlock_bh(&sk->sk_callback_lock);
  523. ready(sk);
  524. }
  525. /* see o2net_register_callbacks() */
  526. static void o2net_state_change(struct sock *sk)
  527. {
  528. void (*state_change)(struct sock *sk);
  529. struct o2net_sock_container *sc;
  530. read_lock_bh(&sk->sk_callback_lock);
  531. sc = sk->sk_user_data;
  532. if (sc == NULL) {
  533. state_change = sk->sk_state_change;
  534. goto out;
  535. }
  536. sclog(sc, "state_change to %d\n", sk->sk_state);
  537. state_change = sc->sc_state_change;
  538. switch(sk->sk_state) {
  539. /* ignore connecting sockets as they make progress */
  540. case TCP_SYN_SENT:
  541. case TCP_SYN_RECV:
  542. break;
  543. case TCP_ESTABLISHED:
  544. o2net_sc_queue_work(sc, &sc->sc_connect_work);
  545. break;
  546. default:
  547. printk(KERN_INFO "o2net: Connection to " SC_NODEF_FMT
  548. " shutdown, state %d\n",
  549. SC_NODEF_ARGS(sc), sk->sk_state);
  550. o2net_sc_queue_work(sc, &sc->sc_shutdown_work);
  551. break;
  552. }
  553. out:
  554. read_unlock_bh(&sk->sk_callback_lock);
  555. state_change(sk);
  556. }
  557. /*
  558. * we register callbacks so we can queue work on events before calling
  559. * the original callbacks. our callbacks our careful to test user_data
  560. * to discover when they've reaced with o2net_unregister_callbacks().
  561. */
  562. static void o2net_register_callbacks(struct sock *sk,
  563. struct o2net_sock_container *sc)
  564. {
  565. write_lock_bh(&sk->sk_callback_lock);
  566. /* accepted sockets inherit the old listen socket data ready */
  567. if (sk->sk_data_ready == o2net_listen_data_ready) {
  568. sk->sk_data_ready = sk->sk_user_data;
  569. sk->sk_user_data = NULL;
  570. }
  571. BUG_ON(sk->sk_user_data != NULL);
  572. sk->sk_user_data = sc;
  573. sc_get(sc);
  574. sc->sc_data_ready = sk->sk_data_ready;
  575. sc->sc_state_change = sk->sk_state_change;
  576. sk->sk_data_ready = o2net_data_ready;
  577. sk->sk_state_change = o2net_state_change;
  578. mutex_init(&sc->sc_send_lock);
  579. write_unlock_bh(&sk->sk_callback_lock);
  580. }
  581. static int o2net_unregister_callbacks(struct sock *sk,
  582. struct o2net_sock_container *sc)
  583. {
  584. int ret = 0;
  585. write_lock_bh(&sk->sk_callback_lock);
  586. if (sk->sk_user_data == sc) {
  587. ret = 1;
  588. sk->sk_user_data = NULL;
  589. sk->sk_data_ready = sc->sc_data_ready;
  590. sk->sk_state_change = sc->sc_state_change;
  591. }
  592. write_unlock_bh(&sk->sk_callback_lock);
  593. return ret;
  594. }
  595. /*
  596. * this is a little helper that is called by callers who have seen a problem
  597. * with an sc and want to detach it from the nn if someone already hasn't beat
  598. * them to it. if an error is given then the shutdown will be persistent
  599. * and pending transmits will be canceled.
  600. */
  601. static void o2net_ensure_shutdown(struct o2net_node *nn,
  602. struct o2net_sock_container *sc,
  603. int err)
  604. {
  605. spin_lock(&nn->nn_lock);
  606. if (nn->nn_sc == sc)
  607. o2net_set_nn_state(nn, NULL, 0, err);
  608. spin_unlock(&nn->nn_lock);
  609. }
  610. /*
  611. * This work queue function performs the blocking parts of socket shutdown. A
  612. * few paths lead here. set_nn_state will trigger this callback if it sees an
  613. * sc detached from the nn. state_change will also trigger this callback
  614. * directly when it sees errors. In that case we need to call set_nn_state
  615. * ourselves as state_change couldn't get the nn_lock and call set_nn_state
  616. * itself.
  617. */
  618. static void o2net_shutdown_sc(struct work_struct *work)
  619. {
  620. struct o2net_sock_container *sc =
  621. container_of(work, struct o2net_sock_container,
  622. sc_shutdown_work);
  623. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  624. sclog(sc, "shutting down\n");
  625. /* drop the callbacks ref and call shutdown only once */
  626. if (o2net_unregister_callbacks(sc->sc_sock->sk, sc)) {
  627. /* we shouldn't flush as we're in the thread, the
  628. * races with pending sc work structs are harmless */
  629. del_timer_sync(&sc->sc_idle_timeout);
  630. o2net_sc_cancel_delayed_work(sc, &sc->sc_keepalive_work);
  631. sc_put(sc);
  632. kernel_sock_shutdown(sc->sc_sock, SHUT_RDWR);
  633. }
  634. /* not fatal so failed connects before the other guy has our
  635. * heartbeat can be retried */
  636. o2net_ensure_shutdown(nn, sc, 0);
  637. sc_put(sc);
  638. }
  639. /* ------------------------------------------------------------ */
  640. static int o2net_handler_cmp(struct o2net_msg_handler *nmh, u32 msg_type,
  641. u32 key)
  642. {
  643. int ret = memcmp(&nmh->nh_key, &key, sizeof(key));
  644. if (ret == 0)
  645. ret = memcmp(&nmh->nh_msg_type, &msg_type, sizeof(msg_type));
  646. return ret;
  647. }
  648. static struct o2net_msg_handler *
  649. o2net_handler_tree_lookup(u32 msg_type, u32 key, struct rb_node ***ret_p,
  650. struct rb_node **ret_parent)
  651. {
  652. struct rb_node **p = &o2net_handler_tree.rb_node;
  653. struct rb_node *parent = NULL;
  654. struct o2net_msg_handler *nmh, *ret = NULL;
  655. int cmp;
  656. while (*p) {
  657. parent = *p;
  658. nmh = rb_entry(parent, struct o2net_msg_handler, nh_node);
  659. cmp = o2net_handler_cmp(nmh, msg_type, key);
  660. if (cmp < 0)
  661. p = &(*p)->rb_left;
  662. else if (cmp > 0)
  663. p = &(*p)->rb_right;
  664. else {
  665. ret = nmh;
  666. break;
  667. }
  668. }
  669. if (ret_p != NULL)
  670. *ret_p = p;
  671. if (ret_parent != NULL)
  672. *ret_parent = parent;
  673. return ret;
  674. }
  675. static void o2net_handler_kref_release(struct kref *kref)
  676. {
  677. struct o2net_msg_handler *nmh;
  678. nmh = container_of(kref, struct o2net_msg_handler, nh_kref);
  679. kfree(nmh);
  680. }
  681. static void o2net_handler_put(struct o2net_msg_handler *nmh)
  682. {
  683. kref_put(&nmh->nh_kref, o2net_handler_kref_release);
  684. }
  685. /* max_len is protection for the handler func. incoming messages won't
  686. * be given to the handler if their payload is longer than the max. */
  687. int o2net_register_handler(u32 msg_type, u32 key, u32 max_len,
  688. o2net_msg_handler_func *func, void *data,
  689. o2net_post_msg_handler_func *post_func,
  690. struct list_head *unreg_list)
  691. {
  692. struct o2net_msg_handler *nmh = NULL;
  693. struct rb_node **p, *parent;
  694. int ret = 0;
  695. if (max_len > O2NET_MAX_PAYLOAD_BYTES) {
  696. mlog(0, "max_len for message handler out of range: %u\n",
  697. max_len);
  698. ret = -EINVAL;
  699. goto out;
  700. }
  701. if (!msg_type) {
  702. mlog(0, "no message type provided: %u, %p\n", msg_type, func);
  703. ret = -EINVAL;
  704. goto out;
  705. }
  706. if (!func) {
  707. mlog(0, "no message handler provided: %u, %p\n",
  708. msg_type, func);
  709. ret = -EINVAL;
  710. goto out;
  711. }
  712. nmh = kzalloc(sizeof(struct o2net_msg_handler), GFP_NOFS);
  713. if (nmh == NULL) {
  714. ret = -ENOMEM;
  715. goto out;
  716. }
  717. nmh->nh_func = func;
  718. nmh->nh_func_data = data;
  719. nmh->nh_post_func = post_func;
  720. nmh->nh_msg_type = msg_type;
  721. nmh->nh_max_len = max_len;
  722. nmh->nh_key = key;
  723. /* the tree and list get this ref.. they're both removed in
  724. * unregister when this ref is dropped */
  725. kref_init(&nmh->nh_kref);
  726. INIT_LIST_HEAD(&nmh->nh_unregister_item);
  727. write_lock(&o2net_handler_lock);
  728. if (o2net_handler_tree_lookup(msg_type, key, &p, &parent))
  729. ret = -EEXIST;
  730. else {
  731. rb_link_node(&nmh->nh_node, parent, p);
  732. rb_insert_color(&nmh->nh_node, &o2net_handler_tree);
  733. list_add_tail(&nmh->nh_unregister_item, unreg_list);
  734. mlog(ML_TCP, "registered handler func %p type %u key %08x\n",
  735. func, msg_type, key);
  736. /* we've had some trouble with handlers seemingly vanishing. */
  737. mlog_bug_on_msg(o2net_handler_tree_lookup(msg_type, key, &p,
  738. &parent) == NULL,
  739. "couldn't find handler we *just* registered "
  740. "for type %u key %08x\n", msg_type, key);
  741. }
  742. write_unlock(&o2net_handler_lock);
  743. if (ret)
  744. goto out;
  745. out:
  746. if (ret)
  747. kfree(nmh);
  748. return ret;
  749. }
  750. EXPORT_SYMBOL_GPL(o2net_register_handler);
  751. void o2net_unregister_handler_list(struct list_head *list)
  752. {
  753. struct o2net_msg_handler *nmh, *n;
  754. write_lock(&o2net_handler_lock);
  755. list_for_each_entry_safe(nmh, n, list, nh_unregister_item) {
  756. mlog(ML_TCP, "unregistering handler func %p type %u key %08x\n",
  757. nmh->nh_func, nmh->nh_msg_type, nmh->nh_key);
  758. rb_erase(&nmh->nh_node, &o2net_handler_tree);
  759. list_del_init(&nmh->nh_unregister_item);
  760. kref_put(&nmh->nh_kref, o2net_handler_kref_release);
  761. }
  762. write_unlock(&o2net_handler_lock);
  763. }
  764. EXPORT_SYMBOL_GPL(o2net_unregister_handler_list);
  765. static struct o2net_msg_handler *o2net_handler_get(u32 msg_type, u32 key)
  766. {
  767. struct o2net_msg_handler *nmh;
  768. read_lock(&o2net_handler_lock);
  769. nmh = o2net_handler_tree_lookup(msg_type, key, NULL, NULL);
  770. if (nmh)
  771. kref_get(&nmh->nh_kref);
  772. read_unlock(&o2net_handler_lock);
  773. return nmh;
  774. }
  775. /* ------------------------------------------------------------ */
  776. static int o2net_recv_tcp_msg(struct socket *sock, void *data, size_t len)
  777. {
  778. struct kvec vec = { .iov_len = len, .iov_base = data, };
  779. struct msghdr msg = { .msg_flags = MSG_DONTWAIT, };
  780. return kernel_recvmsg(sock, &msg, &vec, 1, len, msg.msg_flags);
  781. }
  782. static int o2net_send_tcp_msg(struct socket *sock, struct kvec *vec,
  783. size_t veclen, size_t total)
  784. {
  785. int ret;
  786. struct msghdr msg = {.msg_flags = 0,};
  787. if (sock == NULL) {
  788. ret = -EINVAL;
  789. goto out;
  790. }
  791. ret = kernel_sendmsg(sock, &msg, vec, veclen, total);
  792. if (likely(ret == total))
  793. return 0;
  794. mlog(ML_ERROR, "sendmsg returned %d instead of %zu\n", ret, total);
  795. if (ret >= 0)
  796. ret = -EPIPE; /* should be smarter, I bet */
  797. out:
  798. mlog(0, "returning error: %d\n", ret);
  799. return ret;
  800. }
  801. static void o2net_sendpage(struct o2net_sock_container *sc,
  802. void *kmalloced_virt,
  803. size_t size)
  804. {
  805. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  806. ssize_t ret;
  807. while (1) {
  808. mutex_lock(&sc->sc_send_lock);
  809. ret = sc->sc_sock->ops->sendpage(sc->sc_sock,
  810. virt_to_page(kmalloced_virt),
  811. (long)kmalloced_virt & ~PAGE_MASK,
  812. size, MSG_DONTWAIT);
  813. mutex_unlock(&sc->sc_send_lock);
  814. if (ret == size)
  815. break;
  816. if (ret == (ssize_t)-EAGAIN) {
  817. mlog(0, "sendpage of size %zu to " SC_NODEF_FMT
  818. " returned EAGAIN\n", size, SC_NODEF_ARGS(sc));
  819. cond_resched();
  820. continue;
  821. }
  822. mlog(ML_ERROR, "sendpage of size %zu to " SC_NODEF_FMT
  823. " failed with %zd\n", size, SC_NODEF_ARGS(sc), ret);
  824. o2net_ensure_shutdown(nn, sc, 0);
  825. break;
  826. }
  827. }
  828. static void o2net_init_msg(struct o2net_msg *msg, u16 data_len, u16 msg_type, u32 key)
  829. {
  830. memset(msg, 0, sizeof(struct o2net_msg));
  831. msg->magic = cpu_to_be16(O2NET_MSG_MAGIC);
  832. msg->data_len = cpu_to_be16(data_len);
  833. msg->msg_type = cpu_to_be16(msg_type);
  834. msg->sys_status = cpu_to_be32(O2NET_ERR_NONE);
  835. msg->status = 0;
  836. msg->key = cpu_to_be32(key);
  837. }
  838. static int o2net_tx_can_proceed(struct o2net_node *nn,
  839. struct o2net_sock_container **sc_ret,
  840. int *error)
  841. {
  842. int ret = 0;
  843. spin_lock(&nn->nn_lock);
  844. if (nn->nn_persistent_error) {
  845. ret = 1;
  846. *sc_ret = NULL;
  847. *error = nn->nn_persistent_error;
  848. } else if (nn->nn_sc_valid) {
  849. kref_get(&nn->nn_sc->sc_kref);
  850. ret = 1;
  851. *sc_ret = nn->nn_sc;
  852. *error = 0;
  853. }
  854. spin_unlock(&nn->nn_lock);
  855. return ret;
  856. }
  857. /* Get a map of all nodes to which this node is currently connected to */
  858. void o2net_fill_node_map(unsigned long *map, unsigned bytes)
  859. {
  860. struct o2net_sock_container *sc;
  861. int node, ret;
  862. BUG_ON(bytes < (BITS_TO_LONGS(O2NM_MAX_NODES) * sizeof(unsigned long)));
  863. memset(map, 0, bytes);
  864. for (node = 0; node < O2NM_MAX_NODES; ++node) {
  865. if (!o2net_tx_can_proceed(o2net_nn_from_num(node), &sc, &ret))
  866. continue;
  867. if (!ret) {
  868. set_bit(node, map);
  869. sc_put(sc);
  870. }
  871. }
  872. }
  873. EXPORT_SYMBOL_GPL(o2net_fill_node_map);
  874. int o2net_send_message_vec(u32 msg_type, u32 key, struct kvec *caller_vec,
  875. size_t caller_veclen, u8 target_node, int *status)
  876. {
  877. int ret = 0;
  878. struct o2net_msg *msg = NULL;
  879. size_t veclen, caller_bytes = 0;
  880. struct kvec *vec = NULL;
  881. struct o2net_sock_container *sc = NULL;
  882. struct o2net_node *nn = o2net_nn_from_num(target_node);
  883. struct o2net_status_wait nsw = {
  884. .ns_node_item = LIST_HEAD_INIT(nsw.ns_node_item),
  885. };
  886. struct o2net_send_tracking nst;
  887. o2net_init_nst(&nst, msg_type, key, current, target_node);
  888. if (o2net_wq == NULL) {
  889. mlog(0, "attempt to tx without o2netd running\n");
  890. ret = -ESRCH;
  891. goto out;
  892. }
  893. if (caller_veclen == 0) {
  894. mlog(0, "bad kvec array length\n");
  895. ret = -EINVAL;
  896. goto out;
  897. }
  898. caller_bytes = iov_length((struct iovec *)caller_vec, caller_veclen);
  899. if (caller_bytes > O2NET_MAX_PAYLOAD_BYTES) {
  900. mlog(0, "total payload len %zu too large\n", caller_bytes);
  901. ret = -EINVAL;
  902. goto out;
  903. }
  904. if (target_node == o2nm_this_node()) {
  905. ret = -ELOOP;
  906. goto out;
  907. }
  908. o2net_debug_add_nst(&nst);
  909. o2net_set_nst_sock_time(&nst);
  910. wait_event(nn->nn_sc_wq, o2net_tx_can_proceed(nn, &sc, &ret));
  911. if (ret)
  912. goto out;
  913. o2net_set_nst_sock_container(&nst, sc);
  914. veclen = caller_veclen + 1;
  915. vec = kmalloc(sizeof(struct kvec) * veclen, GFP_ATOMIC);
  916. if (vec == NULL) {
  917. mlog(0, "failed to %zu element kvec!\n", veclen);
  918. ret = -ENOMEM;
  919. goto out;
  920. }
  921. msg = kmalloc(sizeof(struct o2net_msg), GFP_ATOMIC);
  922. if (!msg) {
  923. mlog(0, "failed to allocate a o2net_msg!\n");
  924. ret = -ENOMEM;
  925. goto out;
  926. }
  927. o2net_init_msg(msg, caller_bytes, msg_type, key);
  928. vec[0].iov_len = sizeof(struct o2net_msg);
  929. vec[0].iov_base = msg;
  930. memcpy(&vec[1], caller_vec, caller_veclen * sizeof(struct kvec));
  931. ret = o2net_prep_nsw(nn, &nsw);
  932. if (ret)
  933. goto out;
  934. msg->msg_num = cpu_to_be32(nsw.ns_id);
  935. o2net_set_nst_msg_id(&nst, nsw.ns_id);
  936. o2net_set_nst_send_time(&nst);
  937. /* finally, convert the message header to network byte-order
  938. * and send */
  939. mutex_lock(&sc->sc_send_lock);
  940. ret = o2net_send_tcp_msg(sc->sc_sock, vec, veclen,
  941. sizeof(struct o2net_msg) + caller_bytes);
  942. mutex_unlock(&sc->sc_send_lock);
  943. msglog(msg, "sending returned %d\n", ret);
  944. if (ret < 0) {
  945. mlog(0, "error returned from o2net_send_tcp_msg=%d\n", ret);
  946. goto out;
  947. }
  948. /* wait on other node's handler */
  949. o2net_set_nst_status_time(&nst);
  950. wait_event(nsw.ns_wq, o2net_nsw_completed(nn, &nsw));
  951. o2net_update_send_stats(&nst, sc);
  952. /* Note that we avoid overwriting the callers status return
  953. * variable if a system error was reported on the other
  954. * side. Callers beware. */
  955. ret = o2net_sys_err_to_errno(nsw.ns_sys_status);
  956. if (status && !ret)
  957. *status = nsw.ns_status;
  958. mlog(0, "woken, returning system status %d, user status %d\n",
  959. ret, nsw.ns_status);
  960. out:
  961. o2net_debug_del_nst(&nst); /* must be before dropping sc and node */
  962. if (sc)
  963. sc_put(sc);
  964. kfree(vec);
  965. kfree(msg);
  966. o2net_complete_nsw(nn, &nsw, 0, 0, 0);
  967. return ret;
  968. }
  969. EXPORT_SYMBOL_GPL(o2net_send_message_vec);
  970. int o2net_send_message(u32 msg_type, u32 key, void *data, u32 len,
  971. u8 target_node, int *status)
  972. {
  973. struct kvec vec = {
  974. .iov_base = data,
  975. .iov_len = len,
  976. };
  977. return o2net_send_message_vec(msg_type, key, &vec, 1,
  978. target_node, status);
  979. }
  980. EXPORT_SYMBOL_GPL(o2net_send_message);
  981. static int o2net_send_status_magic(struct socket *sock, struct o2net_msg *hdr,
  982. enum o2net_system_error syserr, int err)
  983. {
  984. struct kvec vec = {
  985. .iov_base = hdr,
  986. .iov_len = sizeof(struct o2net_msg),
  987. };
  988. BUG_ON(syserr >= O2NET_ERR_MAX);
  989. /* leave other fields intact from the incoming message, msg_num
  990. * in particular */
  991. hdr->sys_status = cpu_to_be32(syserr);
  992. hdr->status = cpu_to_be32(err);
  993. hdr->magic = cpu_to_be16(O2NET_MSG_STATUS_MAGIC); // twiddle the magic
  994. hdr->data_len = 0;
  995. msglog(hdr, "about to send status magic %d\n", err);
  996. /* hdr has been in host byteorder this whole time */
  997. return o2net_send_tcp_msg(sock, &vec, 1, sizeof(struct o2net_msg));
  998. }
  999. /* this returns -errno if the header was unknown or too large, etc.
  1000. * after this is called the buffer us reused for the next message */
  1001. static int o2net_process_message(struct o2net_sock_container *sc,
  1002. struct o2net_msg *hdr)
  1003. {
  1004. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  1005. int ret = 0, handler_status;
  1006. enum o2net_system_error syserr;
  1007. struct o2net_msg_handler *nmh = NULL;
  1008. void *ret_data = NULL;
  1009. msglog(hdr, "processing message\n");
  1010. o2net_sc_postpone_idle(sc);
  1011. switch(be16_to_cpu(hdr->magic)) {
  1012. case O2NET_MSG_STATUS_MAGIC:
  1013. /* special type for returning message status */
  1014. o2net_complete_nsw(nn, NULL,
  1015. be32_to_cpu(hdr->msg_num),
  1016. be32_to_cpu(hdr->sys_status),
  1017. be32_to_cpu(hdr->status));
  1018. goto out;
  1019. case O2NET_MSG_KEEP_REQ_MAGIC:
  1020. o2net_sendpage(sc, o2net_keep_resp,
  1021. sizeof(*o2net_keep_resp));
  1022. goto out;
  1023. case O2NET_MSG_KEEP_RESP_MAGIC:
  1024. goto out;
  1025. case O2NET_MSG_MAGIC:
  1026. break;
  1027. default:
  1028. msglog(hdr, "bad magic\n");
  1029. ret = -EINVAL;
  1030. goto out;
  1031. break;
  1032. }
  1033. /* find a handler for it */
  1034. handler_status = 0;
  1035. nmh = o2net_handler_get(be16_to_cpu(hdr->msg_type),
  1036. be32_to_cpu(hdr->key));
  1037. if (!nmh) {
  1038. mlog(ML_TCP, "couldn't find handler for type %u key %08x\n",
  1039. be16_to_cpu(hdr->msg_type), be32_to_cpu(hdr->key));
  1040. syserr = O2NET_ERR_NO_HNDLR;
  1041. goto out_respond;
  1042. }
  1043. syserr = O2NET_ERR_NONE;
  1044. if (be16_to_cpu(hdr->data_len) > nmh->nh_max_len)
  1045. syserr = O2NET_ERR_OVERFLOW;
  1046. if (syserr != O2NET_ERR_NONE)
  1047. goto out_respond;
  1048. o2net_set_func_start_time(sc);
  1049. sc->sc_msg_key = be32_to_cpu(hdr->key);
  1050. sc->sc_msg_type = be16_to_cpu(hdr->msg_type);
  1051. handler_status = (nmh->nh_func)(hdr, sizeof(struct o2net_msg) +
  1052. be16_to_cpu(hdr->data_len),
  1053. nmh->nh_func_data, &ret_data);
  1054. o2net_set_func_stop_time(sc);
  1055. o2net_update_recv_stats(sc);
  1056. out_respond:
  1057. /* this destroys the hdr, so don't use it after this */
  1058. mutex_lock(&sc->sc_send_lock);
  1059. ret = o2net_send_status_magic(sc->sc_sock, hdr, syserr,
  1060. handler_status);
  1061. mutex_unlock(&sc->sc_send_lock);
  1062. hdr = NULL;
  1063. mlog(0, "sending handler status %d, syserr %d returned %d\n",
  1064. handler_status, syserr, ret);
  1065. if (nmh) {
  1066. BUG_ON(ret_data != NULL && nmh->nh_post_func == NULL);
  1067. if (nmh->nh_post_func)
  1068. (nmh->nh_post_func)(handler_status, nmh->nh_func_data,
  1069. ret_data);
  1070. }
  1071. out:
  1072. if (nmh)
  1073. o2net_handler_put(nmh);
  1074. return ret;
  1075. }
  1076. static int o2net_check_handshake(struct o2net_sock_container *sc)
  1077. {
  1078. struct o2net_handshake *hand = page_address(sc->sc_page);
  1079. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  1080. if (hand->protocol_version != cpu_to_be64(O2NET_PROTOCOL_VERSION)) {
  1081. printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " Advertised net "
  1082. "protocol version %llu but %llu is required. "
  1083. "Disconnecting.\n", SC_NODEF_ARGS(sc),
  1084. (unsigned long long)be64_to_cpu(hand->protocol_version),
  1085. O2NET_PROTOCOL_VERSION);
  1086. /* don't bother reconnecting if its the wrong version. */
  1087. o2net_ensure_shutdown(nn, sc, -ENOTCONN);
  1088. return -1;
  1089. }
  1090. /*
  1091. * Ensure timeouts are consistent with other nodes, otherwise
  1092. * we can end up with one node thinking that the other must be down,
  1093. * but isn't. This can ultimately cause corruption.
  1094. */
  1095. if (be32_to_cpu(hand->o2net_idle_timeout_ms) !=
  1096. o2net_idle_timeout()) {
  1097. printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " uses a network "
  1098. "idle timeout of %u ms, but we use %u ms locally. "
  1099. "Disconnecting.\n", SC_NODEF_ARGS(sc),
  1100. be32_to_cpu(hand->o2net_idle_timeout_ms),
  1101. o2net_idle_timeout());
  1102. o2net_ensure_shutdown(nn, sc, -ENOTCONN);
  1103. return -1;
  1104. }
  1105. if (be32_to_cpu(hand->o2net_keepalive_delay_ms) !=
  1106. o2net_keepalive_delay()) {
  1107. printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " uses a keepalive "
  1108. "delay of %u ms, but we use %u ms locally. "
  1109. "Disconnecting.\n", SC_NODEF_ARGS(sc),
  1110. be32_to_cpu(hand->o2net_keepalive_delay_ms),
  1111. o2net_keepalive_delay());
  1112. o2net_ensure_shutdown(nn, sc, -ENOTCONN);
  1113. return -1;
  1114. }
  1115. if (be32_to_cpu(hand->o2hb_heartbeat_timeout_ms) !=
  1116. O2HB_MAX_WRITE_TIMEOUT_MS) {
  1117. printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " uses a heartbeat "
  1118. "timeout of %u ms, but we use %u ms locally. "
  1119. "Disconnecting.\n", SC_NODEF_ARGS(sc),
  1120. be32_to_cpu(hand->o2hb_heartbeat_timeout_ms),
  1121. O2HB_MAX_WRITE_TIMEOUT_MS);
  1122. o2net_ensure_shutdown(nn, sc, -ENOTCONN);
  1123. return -1;
  1124. }
  1125. sc->sc_handshake_ok = 1;
  1126. spin_lock(&nn->nn_lock);
  1127. /* set valid and queue the idle timers only if it hasn't been
  1128. * shut down already */
  1129. if (nn->nn_sc == sc) {
  1130. o2net_sc_reset_idle_timer(sc);
  1131. atomic_set(&nn->nn_timeout, 0);
  1132. o2net_set_nn_state(nn, sc, 1, 0);
  1133. }
  1134. spin_unlock(&nn->nn_lock);
  1135. /* shift everything up as though it wasn't there */
  1136. sc->sc_page_off -= sizeof(struct o2net_handshake);
  1137. if (sc->sc_page_off)
  1138. memmove(hand, hand + 1, sc->sc_page_off);
  1139. return 0;
  1140. }
  1141. /* this demuxes the queued rx bytes into header or payload bits and calls
  1142. * handlers as each full message is read off the socket. it returns -error,
  1143. * == 0 eof, or > 0 for progress made.*/
  1144. static int o2net_advance_rx(struct o2net_sock_container *sc)
  1145. {
  1146. struct o2net_msg *hdr;
  1147. int ret = 0;
  1148. void *data;
  1149. size_t datalen;
  1150. sclog(sc, "receiving\n");
  1151. o2net_set_advance_start_time(sc);
  1152. if (unlikely(sc->sc_handshake_ok == 0)) {
  1153. if(sc->sc_page_off < sizeof(struct o2net_handshake)) {
  1154. data = page_address(sc->sc_page) + sc->sc_page_off;
  1155. datalen = sizeof(struct o2net_handshake) - sc->sc_page_off;
  1156. ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
  1157. if (ret > 0)
  1158. sc->sc_page_off += ret;
  1159. }
  1160. if (sc->sc_page_off == sizeof(struct o2net_handshake)) {
  1161. o2net_check_handshake(sc);
  1162. if (unlikely(sc->sc_handshake_ok == 0))
  1163. ret = -EPROTO;
  1164. }
  1165. goto out;
  1166. }
  1167. /* do we need more header? */
  1168. if (sc->sc_page_off < sizeof(struct o2net_msg)) {
  1169. data = page_address(sc->sc_page) + sc->sc_page_off;
  1170. datalen = sizeof(struct o2net_msg) - sc->sc_page_off;
  1171. ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
  1172. if (ret > 0) {
  1173. sc->sc_page_off += ret;
  1174. /* only swab incoming here.. we can
  1175. * only get here once as we cross from
  1176. * being under to over */
  1177. if (sc->sc_page_off == sizeof(struct o2net_msg)) {
  1178. hdr = page_address(sc->sc_page);
  1179. if (be16_to_cpu(hdr->data_len) >
  1180. O2NET_MAX_PAYLOAD_BYTES)
  1181. ret = -EOVERFLOW;
  1182. }
  1183. }
  1184. if (ret <= 0)
  1185. goto out;
  1186. }
  1187. if (sc->sc_page_off < sizeof(struct o2net_msg)) {
  1188. /* oof, still don't have a header */
  1189. goto out;
  1190. }
  1191. /* this was swabbed above when we first read it */
  1192. hdr = page_address(sc->sc_page);
  1193. msglog(hdr, "at page_off %zu\n", sc->sc_page_off);
  1194. /* do we need more payload? */
  1195. if (sc->sc_page_off - sizeof(struct o2net_msg) < be16_to_cpu(hdr->data_len)) {
  1196. /* need more payload */
  1197. data = page_address(sc->sc_page) + sc->sc_page_off;
  1198. datalen = (sizeof(struct o2net_msg) + be16_to_cpu(hdr->data_len)) -
  1199. sc->sc_page_off;
  1200. ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
  1201. if (ret > 0)
  1202. sc->sc_page_off += ret;
  1203. if (ret <= 0)
  1204. goto out;
  1205. }
  1206. if (sc->sc_page_off - sizeof(struct o2net_msg) == be16_to_cpu(hdr->data_len)) {
  1207. /* we can only get here once, the first time we read
  1208. * the payload.. so set ret to progress if the handler
  1209. * works out. after calling this the message is toast */
  1210. ret = o2net_process_message(sc, hdr);
  1211. if (ret == 0)
  1212. ret = 1;
  1213. sc->sc_page_off = 0;
  1214. }
  1215. out:
  1216. sclog(sc, "ret = %d\n", ret);
  1217. o2net_set_advance_stop_time(sc);
  1218. return ret;
  1219. }
  1220. /* this work func is triggerd by data ready. it reads until it can read no
  1221. * more. it interprets 0, eof, as fatal. if data_ready hits while we're doing
  1222. * our work the work struct will be marked and we'll be called again. */
  1223. static void o2net_rx_until_empty(struct work_struct *work)
  1224. {
  1225. struct o2net_sock_container *sc =
  1226. container_of(work, struct o2net_sock_container, sc_rx_work);
  1227. int ret;
  1228. do {
  1229. ret = o2net_advance_rx(sc);
  1230. } while (ret > 0);
  1231. if (ret <= 0 && ret != -EAGAIN) {
  1232. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  1233. sclog(sc, "saw error %d, closing\n", ret);
  1234. /* not permanent so read failed handshake can retry */
  1235. o2net_ensure_shutdown(nn, sc, 0);
  1236. }
  1237. sc_put(sc);
  1238. }
  1239. static int o2net_set_nodelay(struct socket *sock)
  1240. {
  1241. int ret, val = 1;
  1242. mm_segment_t oldfs;
  1243. oldfs = get_fs();
  1244. set_fs(KERNEL_DS);
  1245. /*
  1246. * Dear unsuspecting programmer,
  1247. *
  1248. * Don't use sock_setsockopt() for SOL_TCP. It doesn't check its level
  1249. * argument and assumes SOL_SOCKET so, say, your TCP_NODELAY will
  1250. * silently turn into SO_DEBUG.
  1251. *
  1252. * Yours,
  1253. * Keeper of hilariously fragile interfaces.
  1254. */
  1255. ret = sock->ops->setsockopt(sock, SOL_TCP, TCP_NODELAY,
  1256. (char __user *)&val, sizeof(val));
  1257. set_fs(oldfs);
  1258. return ret;
  1259. }
  1260. static int o2net_set_usertimeout(struct socket *sock)
  1261. {
  1262. int user_timeout = O2NET_TCP_USER_TIMEOUT;
  1263. return kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
  1264. (char *)&user_timeout, sizeof(user_timeout));
  1265. }
  1266. static void o2net_initialize_handshake(void)
  1267. {
  1268. o2net_hand->o2hb_heartbeat_timeout_ms = cpu_to_be32(
  1269. O2HB_MAX_WRITE_TIMEOUT_MS);
  1270. o2net_hand->o2net_idle_timeout_ms = cpu_to_be32(o2net_idle_timeout());
  1271. o2net_hand->o2net_keepalive_delay_ms = cpu_to_be32(
  1272. o2net_keepalive_delay());
  1273. o2net_hand->o2net_reconnect_delay_ms = cpu_to_be32(
  1274. o2net_reconnect_delay());
  1275. }
  1276. /* ------------------------------------------------------------ */
  1277. /* called when a connect completes and after a sock is accepted. the
  1278. * rx path will see the response and mark the sc valid */
  1279. static void o2net_sc_connect_completed(struct work_struct *work)
  1280. {
  1281. struct o2net_sock_container *sc =
  1282. container_of(work, struct o2net_sock_container,
  1283. sc_connect_work);
  1284. mlog(ML_MSG, "sc sending handshake with ver %llu id %llx\n",
  1285. (unsigned long long)O2NET_PROTOCOL_VERSION,
  1286. (unsigned long long)be64_to_cpu(o2net_hand->connector_id));
  1287. o2net_initialize_handshake();
  1288. o2net_sendpage(sc, o2net_hand, sizeof(*o2net_hand));
  1289. sc_put(sc);
  1290. }
  1291. /* this is called as a work_struct func. */
  1292. static void o2net_sc_send_keep_req(struct work_struct *work)
  1293. {
  1294. struct o2net_sock_container *sc =
  1295. container_of(work, struct o2net_sock_container,
  1296. sc_keepalive_work.work);
  1297. o2net_sendpage(sc, o2net_keep_req, sizeof(*o2net_keep_req));
  1298. sc_put(sc);
  1299. }
  1300. /* socket shutdown does a del_timer_sync against this as it tears down.
  1301. * we can't start this timer until we've got to the point in sc buildup
  1302. * where shutdown is going to be involved */
  1303. static void o2net_idle_timer(unsigned long data)
  1304. {
  1305. struct o2net_sock_container *sc = (struct o2net_sock_container *)data;
  1306. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  1307. #ifdef CONFIG_DEBUG_FS
  1308. unsigned long msecs = ktime_to_ms(ktime_get()) -
  1309. ktime_to_ms(sc->sc_tv_timer);
  1310. #else
  1311. unsigned long msecs = o2net_idle_timeout();
  1312. #endif
  1313. printk(KERN_NOTICE "o2net: Connection to " SC_NODEF_FMT " has been "
  1314. "idle for %lu.%lu secs.\n",
  1315. SC_NODEF_ARGS(sc), msecs / 1000, msecs % 1000);
  1316. /* idle timerout happen, don't shutdown the connection, but
  1317. * make fence decision. Maybe the connection can recover before
  1318. * the decision is made.
  1319. */
  1320. atomic_set(&nn->nn_timeout, 1);
  1321. o2quo_conn_err(o2net_num_from_nn(nn));
  1322. queue_delayed_work(o2net_wq, &nn->nn_still_up,
  1323. msecs_to_jiffies(O2NET_QUORUM_DELAY_MS));
  1324. o2net_sc_reset_idle_timer(sc);
  1325. }
  1326. static void o2net_sc_reset_idle_timer(struct o2net_sock_container *sc)
  1327. {
  1328. o2net_sc_cancel_delayed_work(sc, &sc->sc_keepalive_work);
  1329. o2net_sc_queue_delayed_work(sc, &sc->sc_keepalive_work,
  1330. msecs_to_jiffies(o2net_keepalive_delay()));
  1331. o2net_set_sock_timer(sc);
  1332. mod_timer(&sc->sc_idle_timeout,
  1333. jiffies + msecs_to_jiffies(o2net_idle_timeout()));
  1334. }
  1335. static void o2net_sc_postpone_idle(struct o2net_sock_container *sc)
  1336. {
  1337. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  1338. /* clear fence decision since the connection recover from timeout*/
  1339. if (atomic_read(&nn->nn_timeout)) {
  1340. o2quo_conn_up(o2net_num_from_nn(nn));
  1341. cancel_delayed_work(&nn->nn_still_up);
  1342. atomic_set(&nn->nn_timeout, 0);
  1343. }
  1344. /* Only push out an existing timer */
  1345. if (timer_pending(&sc->sc_idle_timeout))
  1346. o2net_sc_reset_idle_timer(sc);
  1347. }
  1348. /* this work func is kicked whenever a path sets the nn state which doesn't
  1349. * have valid set. This includes seeing hb come up, losing a connection,
  1350. * having a connect attempt fail, etc. This centralizes the logic which decides
  1351. * if a connect attempt should be made or if we should give up and all future
  1352. * transmit attempts should fail */
  1353. static void o2net_start_connect(struct work_struct *work)
  1354. {
  1355. struct o2net_node *nn =
  1356. container_of(work, struct o2net_node, nn_connect_work.work);
  1357. struct o2net_sock_container *sc = NULL;
  1358. struct o2nm_node *node = NULL, *mynode = NULL;
  1359. struct socket *sock = NULL;
  1360. struct sockaddr_in myaddr = {0, }, remoteaddr = {0, };
  1361. int ret = 0, stop;
  1362. unsigned int timeout;
  1363. unsigned int noio_flag;
  1364. /*
  1365. * sock_create allocates the sock with GFP_KERNEL. We must set
  1366. * per-process flag PF_MEMALLOC_NOIO so that all allocations done
  1367. * by this process are done as if GFP_NOIO was specified. So we
  1368. * are not reentering filesystem while doing memory reclaim.
  1369. */
  1370. noio_flag = memalloc_noio_save();
  1371. /* if we're greater we initiate tx, otherwise we accept */
  1372. if (o2nm_this_node() <= o2net_num_from_nn(nn))
  1373. goto out;
  1374. /* watch for racing with tearing a node down */
  1375. node = o2nm_get_node_by_num(o2net_num_from_nn(nn));
  1376. if (node == NULL)
  1377. goto out;
  1378. mynode = o2nm_get_node_by_num(o2nm_this_node());
  1379. if (mynode == NULL)
  1380. goto out;
  1381. spin_lock(&nn->nn_lock);
  1382. /*
  1383. * see if we already have one pending or have given up.
  1384. * For nn_timeout, it is set when we close the connection
  1385. * because of the idle time out. So it means that we have
  1386. * at least connected to that node successfully once,
  1387. * now try to connect to it again.
  1388. */
  1389. timeout = atomic_read(&nn->nn_timeout);
  1390. stop = (nn->nn_sc ||
  1391. (nn->nn_persistent_error &&
  1392. (nn->nn_persistent_error != -ENOTCONN || timeout == 0)));
  1393. spin_unlock(&nn->nn_lock);
  1394. if (stop)
  1395. goto out;
  1396. nn->nn_last_connect_attempt = jiffies;
  1397. sc = sc_alloc(node);
  1398. if (sc == NULL) {
  1399. mlog(0, "couldn't allocate sc\n");
  1400. ret = -ENOMEM;
  1401. goto out;
  1402. }
  1403. ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
  1404. if (ret < 0) {
  1405. mlog(0, "can't create socket: %d\n", ret);
  1406. goto out;
  1407. }
  1408. sc->sc_sock = sock; /* freed by sc_kref_release */
  1409. sock->sk->sk_allocation = GFP_ATOMIC;
  1410. myaddr.sin_family = AF_INET;
  1411. myaddr.sin_addr.s_addr = mynode->nd_ipv4_address;
  1412. myaddr.sin_port = htons(0); /* any port */
  1413. ret = sock->ops->bind(sock, (struct sockaddr *)&myaddr,
  1414. sizeof(myaddr));
  1415. if (ret) {
  1416. mlog(ML_ERROR, "bind failed with %d at address %pI4\n",
  1417. ret, &mynode->nd_ipv4_address);
  1418. goto out;
  1419. }
  1420. ret = o2net_set_nodelay(sc->sc_sock);
  1421. if (ret) {
  1422. mlog(ML_ERROR, "setting TCP_NODELAY failed with %d\n", ret);
  1423. goto out;
  1424. }
  1425. ret = o2net_set_usertimeout(sock);
  1426. if (ret) {
  1427. mlog(ML_ERROR, "set TCP_USER_TIMEOUT failed with %d\n", ret);
  1428. goto out;
  1429. }
  1430. o2net_register_callbacks(sc->sc_sock->sk, sc);
  1431. spin_lock(&nn->nn_lock);
  1432. /* handshake completion will set nn->nn_sc_valid */
  1433. o2net_set_nn_state(nn, sc, 0, 0);
  1434. spin_unlock(&nn->nn_lock);
  1435. remoteaddr.sin_family = AF_INET;
  1436. remoteaddr.sin_addr.s_addr = node->nd_ipv4_address;
  1437. remoteaddr.sin_port = node->nd_ipv4_port;
  1438. ret = sc->sc_sock->ops->connect(sc->sc_sock,
  1439. (struct sockaddr *)&remoteaddr,
  1440. sizeof(remoteaddr),
  1441. O_NONBLOCK);
  1442. if (ret == -EINPROGRESS)
  1443. ret = 0;
  1444. out:
  1445. if (ret && sc) {
  1446. printk(KERN_NOTICE "o2net: Connect attempt to " SC_NODEF_FMT
  1447. " failed with errno %d\n", SC_NODEF_ARGS(sc), ret);
  1448. /* 0 err so that another will be queued and attempted
  1449. * from set_nn_state */
  1450. o2net_ensure_shutdown(nn, sc, 0);
  1451. }
  1452. if (sc)
  1453. sc_put(sc);
  1454. if (node)
  1455. o2nm_node_put(node);
  1456. if (mynode)
  1457. o2nm_node_put(mynode);
  1458. memalloc_noio_restore(noio_flag);
  1459. return;
  1460. }
  1461. static void o2net_connect_expired(struct work_struct *work)
  1462. {
  1463. struct o2net_node *nn =
  1464. container_of(work, struct o2net_node, nn_connect_expired.work);
  1465. spin_lock(&nn->nn_lock);
  1466. if (!nn->nn_sc_valid) {
  1467. printk(KERN_NOTICE "o2net: No connection established with "
  1468. "node %u after %u.%u seconds, check network and"
  1469. " cluster configuration.\n",
  1470. o2net_num_from_nn(nn),
  1471. o2net_idle_timeout() / 1000,
  1472. o2net_idle_timeout() % 1000);
  1473. o2net_set_nn_state(nn, NULL, 0, 0);
  1474. }
  1475. spin_unlock(&nn->nn_lock);
  1476. }
  1477. static void o2net_still_up(struct work_struct *work)
  1478. {
  1479. struct o2net_node *nn =
  1480. container_of(work, struct o2net_node, nn_still_up.work);
  1481. o2quo_hb_still_up(o2net_num_from_nn(nn));
  1482. }
  1483. /* ------------------------------------------------------------ */
  1484. void o2net_disconnect_node(struct o2nm_node *node)
  1485. {
  1486. struct o2net_node *nn = o2net_nn_from_num(node->nd_num);
  1487. /* don't reconnect until it's heartbeating again */
  1488. spin_lock(&nn->nn_lock);
  1489. atomic_set(&nn->nn_timeout, 0);
  1490. o2net_set_nn_state(nn, NULL, 0, -ENOTCONN);
  1491. spin_unlock(&nn->nn_lock);
  1492. if (o2net_wq) {
  1493. cancel_delayed_work(&nn->nn_connect_expired);
  1494. cancel_delayed_work(&nn->nn_connect_work);
  1495. cancel_delayed_work(&nn->nn_still_up);
  1496. flush_workqueue(o2net_wq);
  1497. }
  1498. }
  1499. static void o2net_hb_node_down_cb(struct o2nm_node *node, int node_num,
  1500. void *data)
  1501. {
  1502. o2quo_hb_down(node_num);
  1503. if (!node)
  1504. return;
  1505. if (node_num != o2nm_this_node())
  1506. o2net_disconnect_node(node);
  1507. BUG_ON(atomic_read(&o2net_connected_peers) < 0);
  1508. }
  1509. static void o2net_hb_node_up_cb(struct o2nm_node *node, int node_num,
  1510. void *data)
  1511. {
  1512. struct o2net_node *nn = o2net_nn_from_num(node_num);
  1513. o2quo_hb_up(node_num);
  1514. BUG_ON(!node);
  1515. /* ensure an immediate connect attempt */
  1516. nn->nn_last_connect_attempt = jiffies -
  1517. (msecs_to_jiffies(o2net_reconnect_delay()) + 1);
  1518. if (node_num != o2nm_this_node()) {
  1519. /* believe it or not, accept and node hearbeating testing
  1520. * can succeed for this node before we got here.. so
  1521. * only use set_nn_state to clear the persistent error
  1522. * if that hasn't already happened */
  1523. spin_lock(&nn->nn_lock);
  1524. atomic_set(&nn->nn_timeout, 0);
  1525. if (nn->nn_persistent_error)
  1526. o2net_set_nn_state(nn, NULL, 0, 0);
  1527. spin_unlock(&nn->nn_lock);
  1528. }
  1529. }
  1530. void o2net_unregister_hb_callbacks(void)
  1531. {
  1532. o2hb_unregister_callback(NULL, &o2net_hb_up);
  1533. o2hb_unregister_callback(NULL, &o2net_hb_down);
  1534. }
  1535. int o2net_register_hb_callbacks(void)
  1536. {
  1537. int ret;
  1538. o2hb_setup_callback(&o2net_hb_down, O2HB_NODE_DOWN_CB,
  1539. o2net_hb_node_down_cb, NULL, O2NET_HB_PRI);
  1540. o2hb_setup_callback(&o2net_hb_up, O2HB_NODE_UP_CB,
  1541. o2net_hb_node_up_cb, NULL, O2NET_HB_PRI);
  1542. ret = o2hb_register_callback(NULL, &o2net_hb_up);
  1543. if (ret == 0)
  1544. ret = o2hb_register_callback(NULL, &o2net_hb_down);
  1545. if (ret)
  1546. o2net_unregister_hb_callbacks();
  1547. return ret;
  1548. }
  1549. /* ------------------------------------------------------------ */
  1550. static int o2net_accept_one(struct socket *sock, int *more)
  1551. {
  1552. int ret, slen;
  1553. struct sockaddr_in sin;
  1554. struct socket *new_sock = NULL;
  1555. struct o2nm_node *node = NULL;
  1556. struct o2nm_node *local_node = NULL;
  1557. struct o2net_sock_container *sc = NULL;
  1558. struct o2net_node *nn;
  1559. unsigned int noio_flag;
  1560. /*
  1561. * sock_create_lite allocates the sock with GFP_KERNEL. We must set
  1562. * per-process flag PF_MEMALLOC_NOIO so that all allocations done
  1563. * by this process are done as if GFP_NOIO was specified. So we
  1564. * are not reentering filesystem while doing memory reclaim.
  1565. */
  1566. noio_flag = memalloc_noio_save();
  1567. BUG_ON(sock == NULL);
  1568. *more = 0;
  1569. ret = sock_create_lite(sock->sk->sk_family, sock->sk->sk_type,
  1570. sock->sk->sk_protocol, &new_sock);
  1571. if (ret)
  1572. goto out;
  1573. new_sock->type = sock->type;
  1574. new_sock->ops = sock->ops;
  1575. ret = sock->ops->accept(sock, new_sock, O_NONBLOCK);
  1576. if (ret < 0)
  1577. goto out;
  1578. *more = 1;
  1579. new_sock->sk->sk_allocation = GFP_ATOMIC;
  1580. ret = o2net_set_nodelay(new_sock);
  1581. if (ret) {
  1582. mlog(ML_ERROR, "setting TCP_NODELAY failed with %d\n", ret);
  1583. goto out;
  1584. }
  1585. ret = o2net_set_usertimeout(new_sock);
  1586. if (ret) {
  1587. mlog(ML_ERROR, "set TCP_USER_TIMEOUT failed with %d\n", ret);
  1588. goto out;
  1589. }
  1590. slen = sizeof(sin);
  1591. ret = new_sock->ops->getname(new_sock, (struct sockaddr *) &sin,
  1592. &slen, 1);
  1593. if (ret < 0)
  1594. goto out;
  1595. node = o2nm_get_node_by_ip(sin.sin_addr.s_addr);
  1596. if (node == NULL) {
  1597. printk(KERN_NOTICE "o2net: Attempt to connect from unknown "
  1598. "node at %pI4:%d\n", &sin.sin_addr.s_addr,
  1599. ntohs(sin.sin_port));
  1600. ret = -EINVAL;
  1601. goto out;
  1602. }
  1603. if (o2nm_this_node() >= node->nd_num) {
  1604. local_node = o2nm_get_node_by_num(o2nm_this_node());
  1605. if (local_node)
  1606. printk(KERN_NOTICE "o2net: Unexpected connect attempt "
  1607. "seen at node '%s' (%u, %pI4:%d) from "
  1608. "node '%s' (%u, %pI4:%d)\n",
  1609. local_node->nd_name, local_node->nd_num,
  1610. &(local_node->nd_ipv4_address),
  1611. ntohs(local_node->nd_ipv4_port),
  1612. node->nd_name,
  1613. node->nd_num, &sin.sin_addr.s_addr,
  1614. ntohs(sin.sin_port));
  1615. ret = -EINVAL;
  1616. goto out;
  1617. }
  1618. /* this happens all the time when the other node sees our heartbeat
  1619. * and tries to connect before we see their heartbeat */
  1620. if (!o2hb_check_node_heartbeating_from_callback(node->nd_num)) {
  1621. mlog(ML_CONN, "attempt to connect from node '%s' at "
  1622. "%pI4:%d but it isn't heartbeating\n",
  1623. node->nd_name, &sin.sin_addr.s_addr,
  1624. ntohs(sin.sin_port));
  1625. ret = -EINVAL;
  1626. goto out;
  1627. }
  1628. nn = o2net_nn_from_num(node->nd_num);
  1629. spin_lock(&nn->nn_lock);
  1630. if (nn->nn_sc)
  1631. ret = -EBUSY;
  1632. else
  1633. ret = 0;
  1634. spin_unlock(&nn->nn_lock);
  1635. if (ret) {
  1636. printk(KERN_NOTICE "o2net: Attempt to connect from node '%s' "
  1637. "at %pI4:%d but it already has an open connection\n",
  1638. node->nd_name, &sin.sin_addr.s_addr,
  1639. ntohs(sin.sin_port));
  1640. goto out;
  1641. }
  1642. sc = sc_alloc(node);
  1643. if (sc == NULL) {
  1644. ret = -ENOMEM;
  1645. goto out;
  1646. }
  1647. sc->sc_sock = new_sock;
  1648. new_sock = NULL;
  1649. spin_lock(&nn->nn_lock);
  1650. atomic_set(&nn->nn_timeout, 0);
  1651. o2net_set_nn_state(nn, sc, 0, 0);
  1652. spin_unlock(&nn->nn_lock);
  1653. o2net_register_callbacks(sc->sc_sock->sk, sc);
  1654. o2net_sc_queue_work(sc, &sc->sc_rx_work);
  1655. o2net_initialize_handshake();
  1656. o2net_sendpage(sc, o2net_hand, sizeof(*o2net_hand));
  1657. out:
  1658. if (new_sock)
  1659. sock_release(new_sock);
  1660. if (node)
  1661. o2nm_node_put(node);
  1662. if (local_node)
  1663. o2nm_node_put(local_node);
  1664. if (sc)
  1665. sc_put(sc);
  1666. memalloc_noio_restore(noio_flag);
  1667. return ret;
  1668. }
  1669. /*
  1670. * This function is invoked in response to one or more
  1671. * pending accepts at softIRQ level. We must drain the
  1672. * entire que before returning.
  1673. */
  1674. static void o2net_accept_many(struct work_struct *work)
  1675. {
  1676. struct socket *sock = o2net_listen_sock;
  1677. int more;
  1678. int err;
  1679. /*
  1680. * It is critical to note that due to interrupt moderation
  1681. * at the network driver level, we can't assume to get a
  1682. * softIRQ for every single conn since tcp SYN packets
  1683. * can arrive back-to-back, and therefore many pending
  1684. * accepts may result in just 1 softIRQ. If we terminate
  1685. * the o2net_accept_one() loop upon seeing an err, what happens
  1686. * to the rest of the conns in the queue? If no new SYN
  1687. * arrives for hours, no softIRQ will be delivered,
  1688. * and the connections will just sit in the queue.
  1689. */
  1690. for (;;) {
  1691. err = o2net_accept_one(sock, &more);
  1692. if (!more)
  1693. break;
  1694. cond_resched();
  1695. }
  1696. }
  1697. static void o2net_listen_data_ready(struct sock *sk)
  1698. {
  1699. void (*ready)(struct sock *sk);
  1700. read_lock_bh(&sk->sk_callback_lock);
  1701. ready = sk->sk_user_data;
  1702. if (ready == NULL) { /* check for teardown race */
  1703. ready = sk->sk_data_ready;
  1704. goto out;
  1705. }
  1706. /* This callback may called twice when a new connection
  1707. * is being established as a child socket inherits everything
  1708. * from a parent LISTEN socket, including the data_ready cb of
  1709. * the parent. This leads to a hazard. In o2net_accept_one()
  1710. * we are still initializing the child socket but have not
  1711. * changed the inherited data_ready callback yet when
  1712. * data starts arriving.
  1713. * We avoid this hazard by checking the state.
  1714. * For the listening socket, the state will be TCP_LISTEN; for the new
  1715. * socket, will be TCP_ESTABLISHED. Also, in this case,
  1716. * sk->sk_user_data is not a valid function pointer.
  1717. */
  1718. if (sk->sk_state == TCP_LISTEN) {
  1719. queue_work(o2net_wq, &o2net_listen_work);
  1720. } else {
  1721. ready = NULL;
  1722. }
  1723. out:
  1724. read_unlock_bh(&sk->sk_callback_lock);
  1725. if (ready != NULL)
  1726. ready(sk);
  1727. }
  1728. static int o2net_open_listening_sock(__be32 addr, __be16 port)
  1729. {
  1730. struct socket *sock = NULL;
  1731. int ret;
  1732. struct sockaddr_in sin = {
  1733. .sin_family = PF_INET,
  1734. .sin_addr = { .s_addr = addr },
  1735. .sin_port = port,
  1736. };
  1737. ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
  1738. if (ret < 0) {
  1739. printk(KERN_ERR "o2net: Error %d while creating socket\n", ret);
  1740. goto out;
  1741. }
  1742. sock->sk->sk_allocation = GFP_ATOMIC;
  1743. write_lock_bh(&sock->sk->sk_callback_lock);
  1744. sock->sk->sk_user_data = sock->sk->sk_data_ready;
  1745. sock->sk->sk_data_ready = o2net_listen_data_ready;
  1746. write_unlock_bh(&sock->sk->sk_callback_lock);
  1747. o2net_listen_sock = sock;
  1748. INIT_WORK(&o2net_listen_work, o2net_accept_many);
  1749. sock->sk->sk_reuse = SK_CAN_REUSE;
  1750. ret = sock->ops->bind(sock, (struct sockaddr *)&sin, sizeof(sin));
  1751. if (ret < 0) {
  1752. printk(KERN_ERR "o2net: Error %d while binding socket at "
  1753. "%pI4:%u\n", ret, &addr, ntohs(port));
  1754. goto out;
  1755. }
  1756. ret = sock->ops->listen(sock, 64);
  1757. if (ret < 0)
  1758. printk(KERN_ERR "o2net: Error %d while listening on %pI4:%u\n",
  1759. ret, &addr, ntohs(port));
  1760. out:
  1761. if (ret) {
  1762. o2net_listen_sock = NULL;
  1763. if (sock)
  1764. sock_release(sock);
  1765. }
  1766. return ret;
  1767. }
  1768. /*
  1769. * called from node manager when we should bring up our network listening
  1770. * socket. node manager handles all the serialization to only call this
  1771. * once and to match it with o2net_stop_listening(). note,
  1772. * o2nm_this_node() doesn't work yet as we're being called while it
  1773. * is being set up.
  1774. */
  1775. int o2net_start_listening(struct o2nm_node *node)
  1776. {
  1777. int ret = 0;
  1778. BUG_ON(o2net_wq != NULL);
  1779. BUG_ON(o2net_listen_sock != NULL);
  1780. mlog(ML_KTHREAD, "starting o2net thread...\n");
  1781. o2net_wq = alloc_ordered_workqueue("o2net", WQ_MEM_RECLAIM);
  1782. if (o2net_wq == NULL) {
  1783. mlog(ML_ERROR, "unable to launch o2net thread\n");
  1784. return -ENOMEM; /* ? */
  1785. }
  1786. ret = o2net_open_listening_sock(node->nd_ipv4_address,
  1787. node->nd_ipv4_port);
  1788. if (ret) {
  1789. destroy_workqueue(o2net_wq);
  1790. o2net_wq = NULL;
  1791. } else
  1792. o2quo_conn_up(node->nd_num);
  1793. return ret;
  1794. }
  1795. /* again, o2nm_this_node() doesn't work here as we're involved in
  1796. * tearing it down */
  1797. void o2net_stop_listening(struct o2nm_node *node)
  1798. {
  1799. struct socket *sock = o2net_listen_sock;
  1800. size_t i;
  1801. BUG_ON(o2net_wq == NULL);
  1802. BUG_ON(o2net_listen_sock == NULL);
  1803. /* stop the listening socket from generating work */
  1804. write_lock_bh(&sock->sk->sk_callback_lock);
  1805. sock->sk->sk_data_ready = sock->sk->sk_user_data;
  1806. sock->sk->sk_user_data = NULL;
  1807. write_unlock_bh(&sock->sk->sk_callback_lock);
  1808. for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) {
  1809. struct o2nm_node *node = o2nm_get_node_by_num(i);
  1810. if (node) {
  1811. o2net_disconnect_node(node);
  1812. o2nm_node_put(node);
  1813. }
  1814. }
  1815. /* finish all work and tear down the work queue */
  1816. mlog(ML_KTHREAD, "waiting for o2net thread to exit....\n");
  1817. destroy_workqueue(o2net_wq);
  1818. o2net_wq = NULL;
  1819. sock_release(o2net_listen_sock);
  1820. o2net_listen_sock = NULL;
  1821. o2quo_conn_err(node->nd_num);
  1822. }
  1823. /* ------------------------------------------------------------ */
  1824. int o2net_init(void)
  1825. {
  1826. unsigned long i;
  1827. o2quo_init();
  1828. if (o2net_debugfs_init())
  1829. goto out;
  1830. o2net_hand = kzalloc(sizeof(struct o2net_handshake), GFP_KERNEL);
  1831. o2net_keep_req = kzalloc(sizeof(struct o2net_msg), GFP_KERNEL);
  1832. o2net_keep_resp = kzalloc(sizeof(struct o2net_msg), GFP_KERNEL);
  1833. if (!o2net_hand || !o2net_keep_req || !o2net_keep_resp)
  1834. goto out;
  1835. o2net_hand->protocol_version = cpu_to_be64(O2NET_PROTOCOL_VERSION);
  1836. o2net_hand->connector_id = cpu_to_be64(1);
  1837. o2net_keep_req->magic = cpu_to_be16(O2NET_MSG_KEEP_REQ_MAGIC);
  1838. o2net_keep_resp->magic = cpu_to_be16(O2NET_MSG_KEEP_RESP_MAGIC);
  1839. for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) {
  1840. struct o2net_node *nn = o2net_nn_from_num(i);
  1841. atomic_set(&nn->nn_timeout, 0);
  1842. spin_lock_init(&nn->nn_lock);
  1843. INIT_DELAYED_WORK(&nn->nn_connect_work, o2net_start_connect);
  1844. INIT_DELAYED_WORK(&nn->nn_connect_expired,
  1845. o2net_connect_expired);
  1846. INIT_DELAYED_WORK(&nn->nn_still_up, o2net_still_up);
  1847. /* until we see hb from a node we'll return einval */
  1848. nn->nn_persistent_error = -ENOTCONN;
  1849. init_waitqueue_head(&nn->nn_sc_wq);
  1850. idr_init(&nn->nn_status_idr);
  1851. INIT_LIST_HEAD(&nn->nn_status_list);
  1852. }
  1853. return 0;
  1854. out:
  1855. kfree(o2net_hand);
  1856. kfree(o2net_keep_req);
  1857. kfree(o2net_keep_resp);
  1858. o2net_debugfs_exit();
  1859. o2quo_exit();
  1860. return -ENOMEM;
  1861. }
  1862. void o2net_exit(void)
  1863. {
  1864. o2quo_exit();
  1865. kfree(o2net_hand);
  1866. kfree(o2net_keep_req);
  1867. kfree(o2net_keep_resp);
  1868. o2net_debugfs_exit();
  1869. }