tcp.c 58 KB

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