tcp.c 54 KB

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