tcp.c 55 KB

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