tcp.c 54 KB

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