tcp.c 55 KB

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