tcp.c 58 KB

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