tcp.c 58 KB

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