tcp.c 56 KB

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