tcp.c 51 KB

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