tcp.c 58 KB

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