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