tcp.c 60 KB

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