tcp.c 54 KB

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