tcp.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195
  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. out:
  743. if (ret)
  744. kfree(nmh);
  745. return ret;
  746. }
  747. EXPORT_SYMBOL_GPL(o2net_register_handler);
  748. void o2net_unregister_handler_list(struct list_head *list)
  749. {
  750. struct o2net_msg_handler *nmh, *n;
  751. write_lock(&o2net_handler_lock);
  752. list_for_each_entry_safe(nmh, n, list, nh_unregister_item) {
  753. mlog(ML_TCP, "unregistering handler func %p type %u key %08x\n",
  754. nmh->nh_func, nmh->nh_msg_type, nmh->nh_key);
  755. rb_erase(&nmh->nh_node, &o2net_handler_tree);
  756. list_del_init(&nmh->nh_unregister_item);
  757. kref_put(&nmh->nh_kref, o2net_handler_kref_release);
  758. }
  759. write_unlock(&o2net_handler_lock);
  760. }
  761. EXPORT_SYMBOL_GPL(o2net_unregister_handler_list);
  762. static struct o2net_msg_handler *o2net_handler_get(u32 msg_type, u32 key)
  763. {
  764. struct o2net_msg_handler *nmh;
  765. read_lock(&o2net_handler_lock);
  766. nmh = o2net_handler_tree_lookup(msg_type, key, NULL, NULL);
  767. if (nmh)
  768. kref_get(&nmh->nh_kref);
  769. read_unlock(&o2net_handler_lock);
  770. return nmh;
  771. }
  772. /* ------------------------------------------------------------ */
  773. static int o2net_recv_tcp_msg(struct socket *sock, void *data, size_t len)
  774. {
  775. struct kvec vec = { .iov_len = len, .iov_base = data, };
  776. struct msghdr msg = { .msg_flags = MSG_DONTWAIT, };
  777. iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, &vec, 1, len);
  778. return sock_recvmsg(sock, &msg, MSG_DONTWAIT);
  779. }
  780. static int o2net_send_tcp_msg(struct socket *sock, struct kvec *vec,
  781. size_t veclen, size_t total)
  782. {
  783. int ret;
  784. struct msghdr msg = {.msg_flags = 0,};
  785. if (sock == NULL) {
  786. ret = -EINVAL;
  787. goto out;
  788. }
  789. ret = kernel_sendmsg(sock, &msg, vec, veclen, total);
  790. if (likely(ret == total))
  791. return 0;
  792. mlog(ML_ERROR, "sendmsg returned %d instead of %zu\n", ret, total);
  793. if (ret >= 0)
  794. ret = -EPIPE; /* should be smarter, I bet */
  795. out:
  796. mlog(0, "returning error: %d\n", ret);
  797. return ret;
  798. }
  799. static void o2net_sendpage(struct o2net_sock_container *sc,
  800. void *kmalloced_virt,
  801. size_t size)
  802. {
  803. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  804. ssize_t ret;
  805. while (1) {
  806. mutex_lock(&sc->sc_send_lock);
  807. ret = sc->sc_sock->ops->sendpage(sc->sc_sock,
  808. virt_to_page(kmalloced_virt),
  809. offset_in_page(kmalloced_virt),
  810. size, MSG_DONTWAIT);
  811. mutex_unlock(&sc->sc_send_lock);
  812. if (ret == size)
  813. break;
  814. if (ret == (ssize_t)-EAGAIN) {
  815. mlog(0, "sendpage of size %zu to " SC_NODEF_FMT
  816. " returned EAGAIN\n", size, SC_NODEF_ARGS(sc));
  817. cond_resched();
  818. continue;
  819. }
  820. mlog(ML_ERROR, "sendpage of size %zu to " SC_NODEF_FMT
  821. " failed with %zd\n", size, SC_NODEF_ARGS(sc), ret);
  822. o2net_ensure_shutdown(nn, sc, 0);
  823. break;
  824. }
  825. }
  826. static void o2net_init_msg(struct o2net_msg *msg, u16 data_len, u16 msg_type, u32 key)
  827. {
  828. memset(msg, 0, sizeof(struct o2net_msg));
  829. msg->magic = cpu_to_be16(O2NET_MSG_MAGIC);
  830. msg->data_len = cpu_to_be16(data_len);
  831. msg->msg_type = cpu_to_be16(msg_type);
  832. msg->sys_status = cpu_to_be32(O2NET_ERR_NONE);
  833. msg->status = 0;
  834. msg->key = cpu_to_be32(key);
  835. }
  836. static int o2net_tx_can_proceed(struct o2net_node *nn,
  837. struct o2net_sock_container **sc_ret,
  838. int *error)
  839. {
  840. int ret = 0;
  841. spin_lock(&nn->nn_lock);
  842. if (nn->nn_persistent_error) {
  843. ret = 1;
  844. *sc_ret = NULL;
  845. *error = nn->nn_persistent_error;
  846. } else if (nn->nn_sc_valid) {
  847. kref_get(&nn->nn_sc->sc_kref);
  848. ret = 1;
  849. *sc_ret = nn->nn_sc;
  850. *error = 0;
  851. }
  852. spin_unlock(&nn->nn_lock);
  853. return ret;
  854. }
  855. /* Get a map of all nodes to which this node is currently connected to */
  856. void o2net_fill_node_map(unsigned long *map, unsigned bytes)
  857. {
  858. struct o2net_sock_container *sc;
  859. int node, ret;
  860. BUG_ON(bytes < (BITS_TO_LONGS(O2NM_MAX_NODES) * sizeof(unsigned long)));
  861. memset(map, 0, bytes);
  862. for (node = 0; node < O2NM_MAX_NODES; ++node) {
  863. if (!o2net_tx_can_proceed(o2net_nn_from_num(node), &sc, &ret))
  864. continue;
  865. if (!ret) {
  866. set_bit(node, map);
  867. sc_put(sc);
  868. }
  869. }
  870. }
  871. EXPORT_SYMBOL_GPL(o2net_fill_node_map);
  872. int o2net_send_message_vec(u32 msg_type, u32 key, struct kvec *caller_vec,
  873. size_t caller_veclen, u8 target_node, int *status)
  874. {
  875. int ret = 0;
  876. struct o2net_msg *msg = NULL;
  877. size_t veclen, caller_bytes = 0;
  878. struct kvec *vec = NULL;
  879. struct o2net_sock_container *sc = NULL;
  880. struct o2net_node *nn = o2net_nn_from_num(target_node);
  881. struct o2net_status_wait nsw = {
  882. .ns_node_item = LIST_HEAD_INIT(nsw.ns_node_item),
  883. };
  884. struct o2net_send_tracking nst;
  885. o2net_init_nst(&nst, msg_type, key, current, target_node);
  886. if (o2net_wq == NULL) {
  887. mlog(0, "attempt to tx without o2netd running\n");
  888. ret = -ESRCH;
  889. goto out;
  890. }
  891. if (caller_veclen == 0) {
  892. mlog(0, "bad kvec array length\n");
  893. ret = -EINVAL;
  894. goto out;
  895. }
  896. caller_bytes = iov_length((struct iovec *)caller_vec, caller_veclen);
  897. if (caller_bytes > O2NET_MAX_PAYLOAD_BYTES) {
  898. mlog(0, "total payload len %zu too large\n", caller_bytes);
  899. ret = -EINVAL;
  900. goto out;
  901. }
  902. if (target_node == o2nm_this_node()) {
  903. ret = -ELOOP;
  904. goto out;
  905. }
  906. o2net_debug_add_nst(&nst);
  907. o2net_set_nst_sock_time(&nst);
  908. wait_event(nn->nn_sc_wq, o2net_tx_can_proceed(nn, &sc, &ret));
  909. if (ret)
  910. goto out;
  911. o2net_set_nst_sock_container(&nst, sc);
  912. veclen = caller_veclen + 1;
  913. vec = kmalloc_array(veclen, sizeof(struct kvec), GFP_ATOMIC);
  914. if (vec == NULL) {
  915. mlog(0, "failed to %zu element kvec!\n", veclen);
  916. ret = -ENOMEM;
  917. goto out;
  918. }
  919. msg = kmalloc(sizeof(struct o2net_msg), GFP_ATOMIC);
  920. if (!msg) {
  921. mlog(0, "failed to allocate a o2net_msg!\n");
  922. ret = -ENOMEM;
  923. goto out;
  924. }
  925. o2net_init_msg(msg, caller_bytes, msg_type, key);
  926. vec[0].iov_len = sizeof(struct o2net_msg);
  927. vec[0].iov_base = msg;
  928. memcpy(&vec[1], caller_vec, caller_veclen * sizeof(struct kvec));
  929. ret = o2net_prep_nsw(nn, &nsw);
  930. if (ret)
  931. goto out;
  932. msg->msg_num = cpu_to_be32(nsw.ns_id);
  933. o2net_set_nst_msg_id(&nst, nsw.ns_id);
  934. o2net_set_nst_send_time(&nst);
  935. /* finally, convert the message header to network byte-order
  936. * and send */
  937. mutex_lock(&sc->sc_send_lock);
  938. ret = o2net_send_tcp_msg(sc->sc_sock, vec, veclen,
  939. sizeof(struct o2net_msg) + caller_bytes);
  940. mutex_unlock(&sc->sc_send_lock);
  941. msglog(msg, "sending returned %d\n", ret);
  942. if (ret < 0) {
  943. mlog(0, "error returned from o2net_send_tcp_msg=%d\n", ret);
  944. goto out;
  945. }
  946. /* wait on other node's handler */
  947. o2net_set_nst_status_time(&nst);
  948. wait_event(nsw.ns_wq, o2net_nsw_completed(nn, &nsw));
  949. o2net_update_send_stats(&nst, sc);
  950. /* Note that we avoid overwriting the callers status return
  951. * variable if a system error was reported on the other
  952. * side. Callers beware. */
  953. ret = o2net_sys_err_to_errno(nsw.ns_sys_status);
  954. if (status && !ret)
  955. *status = nsw.ns_status;
  956. mlog(0, "woken, returning system status %d, user status %d\n",
  957. ret, nsw.ns_status);
  958. out:
  959. o2net_debug_del_nst(&nst); /* must be before dropping sc and node */
  960. if (sc)
  961. sc_put(sc);
  962. kfree(vec);
  963. kfree(msg);
  964. o2net_complete_nsw(nn, &nsw, 0, 0, 0);
  965. return ret;
  966. }
  967. EXPORT_SYMBOL_GPL(o2net_send_message_vec);
  968. int o2net_send_message(u32 msg_type, u32 key, void *data, u32 len,
  969. u8 target_node, int *status)
  970. {
  971. struct kvec vec = {
  972. .iov_base = data,
  973. .iov_len = len,
  974. };
  975. return o2net_send_message_vec(msg_type, key, &vec, 1,
  976. target_node, status);
  977. }
  978. EXPORT_SYMBOL_GPL(o2net_send_message);
  979. static int o2net_send_status_magic(struct socket *sock, struct o2net_msg *hdr,
  980. enum o2net_system_error syserr, int err)
  981. {
  982. struct kvec vec = {
  983. .iov_base = hdr,
  984. .iov_len = sizeof(struct o2net_msg),
  985. };
  986. BUG_ON(syserr >= O2NET_ERR_MAX);
  987. /* leave other fields intact from the incoming message, msg_num
  988. * in particular */
  989. hdr->sys_status = cpu_to_be32(syserr);
  990. hdr->status = cpu_to_be32(err);
  991. hdr->magic = cpu_to_be16(O2NET_MSG_STATUS_MAGIC); // twiddle the magic
  992. hdr->data_len = 0;
  993. msglog(hdr, "about to send status magic %d\n", err);
  994. /* hdr has been in host byteorder this whole time */
  995. return o2net_send_tcp_msg(sock, &vec, 1, sizeof(struct o2net_msg));
  996. }
  997. /* this returns -errno if the header was unknown or too large, etc.
  998. * after this is called the buffer us reused for the next message */
  999. static int o2net_process_message(struct o2net_sock_container *sc,
  1000. struct o2net_msg *hdr)
  1001. {
  1002. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  1003. int ret = 0, handler_status;
  1004. enum o2net_system_error syserr;
  1005. struct o2net_msg_handler *nmh = NULL;
  1006. void *ret_data = NULL;
  1007. msglog(hdr, "processing message\n");
  1008. o2net_sc_postpone_idle(sc);
  1009. switch(be16_to_cpu(hdr->magic)) {
  1010. case O2NET_MSG_STATUS_MAGIC:
  1011. /* special type for returning message status */
  1012. o2net_complete_nsw(nn, NULL,
  1013. be32_to_cpu(hdr->msg_num),
  1014. be32_to_cpu(hdr->sys_status),
  1015. be32_to_cpu(hdr->status));
  1016. goto out;
  1017. case O2NET_MSG_KEEP_REQ_MAGIC:
  1018. o2net_sendpage(sc, o2net_keep_resp,
  1019. sizeof(*o2net_keep_resp));
  1020. goto out;
  1021. case O2NET_MSG_KEEP_RESP_MAGIC:
  1022. goto out;
  1023. case O2NET_MSG_MAGIC:
  1024. break;
  1025. default:
  1026. msglog(hdr, "bad magic\n");
  1027. ret = -EINVAL;
  1028. goto out;
  1029. break;
  1030. }
  1031. /* find a handler for it */
  1032. handler_status = 0;
  1033. nmh = o2net_handler_get(be16_to_cpu(hdr->msg_type),
  1034. be32_to_cpu(hdr->key));
  1035. if (!nmh) {
  1036. mlog(ML_TCP, "couldn't find handler for type %u key %08x\n",
  1037. be16_to_cpu(hdr->msg_type), be32_to_cpu(hdr->key));
  1038. syserr = O2NET_ERR_NO_HNDLR;
  1039. goto out_respond;
  1040. }
  1041. syserr = O2NET_ERR_NONE;
  1042. if (be16_to_cpu(hdr->data_len) > nmh->nh_max_len)
  1043. syserr = O2NET_ERR_OVERFLOW;
  1044. if (syserr != O2NET_ERR_NONE)
  1045. goto out_respond;
  1046. o2net_set_func_start_time(sc);
  1047. sc->sc_msg_key = be32_to_cpu(hdr->key);
  1048. sc->sc_msg_type = be16_to_cpu(hdr->msg_type);
  1049. handler_status = (nmh->nh_func)(hdr, sizeof(struct o2net_msg) +
  1050. be16_to_cpu(hdr->data_len),
  1051. nmh->nh_func_data, &ret_data);
  1052. o2net_set_func_stop_time(sc);
  1053. o2net_update_recv_stats(sc);
  1054. out_respond:
  1055. /* this destroys the hdr, so don't use it after this */
  1056. mutex_lock(&sc->sc_send_lock);
  1057. ret = o2net_send_status_magic(sc->sc_sock, hdr, syserr,
  1058. handler_status);
  1059. mutex_unlock(&sc->sc_send_lock);
  1060. hdr = NULL;
  1061. mlog(0, "sending handler status %d, syserr %d returned %d\n",
  1062. handler_status, syserr, ret);
  1063. if (nmh) {
  1064. BUG_ON(ret_data != NULL && nmh->nh_post_func == NULL);
  1065. if (nmh->nh_post_func)
  1066. (nmh->nh_post_func)(handler_status, nmh->nh_func_data,
  1067. ret_data);
  1068. }
  1069. out:
  1070. if (nmh)
  1071. o2net_handler_put(nmh);
  1072. return ret;
  1073. }
  1074. static int o2net_check_handshake(struct o2net_sock_container *sc)
  1075. {
  1076. struct o2net_handshake *hand = page_address(sc->sc_page);
  1077. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  1078. if (hand->protocol_version != cpu_to_be64(O2NET_PROTOCOL_VERSION)) {
  1079. printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " Advertised net "
  1080. "protocol version %llu but %llu is required. "
  1081. "Disconnecting.\n", SC_NODEF_ARGS(sc),
  1082. (unsigned long long)be64_to_cpu(hand->protocol_version),
  1083. O2NET_PROTOCOL_VERSION);
  1084. /* don't bother reconnecting if its the wrong version. */
  1085. o2net_ensure_shutdown(nn, sc, -ENOTCONN);
  1086. return -1;
  1087. }
  1088. /*
  1089. * Ensure timeouts are consistent with other nodes, otherwise
  1090. * we can end up with one node thinking that the other must be down,
  1091. * but isn't. This can ultimately cause corruption.
  1092. */
  1093. if (be32_to_cpu(hand->o2net_idle_timeout_ms) !=
  1094. o2net_idle_timeout()) {
  1095. printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " uses a network "
  1096. "idle timeout of %u ms, but we use %u ms locally. "
  1097. "Disconnecting.\n", SC_NODEF_ARGS(sc),
  1098. be32_to_cpu(hand->o2net_idle_timeout_ms),
  1099. o2net_idle_timeout());
  1100. o2net_ensure_shutdown(nn, sc, -ENOTCONN);
  1101. return -1;
  1102. }
  1103. if (be32_to_cpu(hand->o2net_keepalive_delay_ms) !=
  1104. o2net_keepalive_delay()) {
  1105. printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " uses a keepalive "
  1106. "delay of %u ms, but we use %u ms locally. "
  1107. "Disconnecting.\n", SC_NODEF_ARGS(sc),
  1108. be32_to_cpu(hand->o2net_keepalive_delay_ms),
  1109. o2net_keepalive_delay());
  1110. o2net_ensure_shutdown(nn, sc, -ENOTCONN);
  1111. return -1;
  1112. }
  1113. if (be32_to_cpu(hand->o2hb_heartbeat_timeout_ms) !=
  1114. O2HB_MAX_WRITE_TIMEOUT_MS) {
  1115. printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " uses a heartbeat "
  1116. "timeout of %u ms, but we use %u ms locally. "
  1117. "Disconnecting.\n", SC_NODEF_ARGS(sc),
  1118. be32_to_cpu(hand->o2hb_heartbeat_timeout_ms),
  1119. O2HB_MAX_WRITE_TIMEOUT_MS);
  1120. o2net_ensure_shutdown(nn, sc, -ENOTCONN);
  1121. return -1;
  1122. }
  1123. sc->sc_handshake_ok = 1;
  1124. spin_lock(&nn->nn_lock);
  1125. /* set valid and queue the idle timers only if it hasn't been
  1126. * shut down already */
  1127. if (nn->nn_sc == sc) {
  1128. o2net_sc_reset_idle_timer(sc);
  1129. atomic_set(&nn->nn_timeout, 0);
  1130. o2net_set_nn_state(nn, sc, 1, 0);
  1131. }
  1132. spin_unlock(&nn->nn_lock);
  1133. /* shift everything up as though it wasn't there */
  1134. sc->sc_page_off -= sizeof(struct o2net_handshake);
  1135. if (sc->sc_page_off)
  1136. memmove(hand, hand + 1, sc->sc_page_off);
  1137. return 0;
  1138. }
  1139. /* this demuxes the queued rx bytes into header or payload bits and calls
  1140. * handlers as each full message is read off the socket. it returns -error,
  1141. * == 0 eof, or > 0 for progress made.*/
  1142. static int o2net_advance_rx(struct o2net_sock_container *sc)
  1143. {
  1144. struct o2net_msg *hdr;
  1145. int ret = 0;
  1146. void *data;
  1147. size_t datalen;
  1148. sclog(sc, "receiving\n");
  1149. o2net_set_advance_start_time(sc);
  1150. if (unlikely(sc->sc_handshake_ok == 0)) {
  1151. if(sc->sc_page_off < sizeof(struct o2net_handshake)) {
  1152. data = page_address(sc->sc_page) + sc->sc_page_off;
  1153. datalen = sizeof(struct o2net_handshake) - sc->sc_page_off;
  1154. ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
  1155. if (ret > 0)
  1156. sc->sc_page_off += ret;
  1157. }
  1158. if (sc->sc_page_off == sizeof(struct o2net_handshake)) {
  1159. o2net_check_handshake(sc);
  1160. if (unlikely(sc->sc_handshake_ok == 0))
  1161. ret = -EPROTO;
  1162. }
  1163. goto out;
  1164. }
  1165. /* do we need more header? */
  1166. if (sc->sc_page_off < sizeof(struct o2net_msg)) {
  1167. data = page_address(sc->sc_page) + sc->sc_page_off;
  1168. datalen = sizeof(struct o2net_msg) - sc->sc_page_off;
  1169. ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
  1170. if (ret > 0) {
  1171. sc->sc_page_off += ret;
  1172. /* only swab incoming here.. we can
  1173. * only get here once as we cross from
  1174. * being under to over */
  1175. if (sc->sc_page_off == sizeof(struct o2net_msg)) {
  1176. hdr = page_address(sc->sc_page);
  1177. if (be16_to_cpu(hdr->data_len) >
  1178. O2NET_MAX_PAYLOAD_BYTES)
  1179. ret = -EOVERFLOW;
  1180. }
  1181. }
  1182. if (ret <= 0)
  1183. goto out;
  1184. }
  1185. if (sc->sc_page_off < sizeof(struct o2net_msg)) {
  1186. /* oof, still don't have a header */
  1187. goto out;
  1188. }
  1189. /* this was swabbed above when we first read it */
  1190. hdr = page_address(sc->sc_page);
  1191. msglog(hdr, "at page_off %zu\n", sc->sc_page_off);
  1192. /* do we need more payload? */
  1193. if (sc->sc_page_off - sizeof(struct o2net_msg) < be16_to_cpu(hdr->data_len)) {
  1194. /* need more payload */
  1195. data = page_address(sc->sc_page) + sc->sc_page_off;
  1196. datalen = (sizeof(struct o2net_msg) + be16_to_cpu(hdr->data_len)) -
  1197. sc->sc_page_off;
  1198. ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
  1199. if (ret > 0)
  1200. sc->sc_page_off += ret;
  1201. if (ret <= 0)
  1202. goto out;
  1203. }
  1204. if (sc->sc_page_off - sizeof(struct o2net_msg) == be16_to_cpu(hdr->data_len)) {
  1205. /* we can only get here once, the first time we read
  1206. * the payload.. so set ret to progress if the handler
  1207. * works out. after calling this the message is toast */
  1208. ret = o2net_process_message(sc, hdr);
  1209. if (ret == 0)
  1210. ret = 1;
  1211. sc->sc_page_off = 0;
  1212. }
  1213. out:
  1214. sclog(sc, "ret = %d\n", ret);
  1215. o2net_set_advance_stop_time(sc);
  1216. return ret;
  1217. }
  1218. /* this work func is triggerd by data ready. it reads until it can read no
  1219. * more. it interprets 0, eof, as fatal. if data_ready hits while we're doing
  1220. * our work the work struct will be marked and we'll be called again. */
  1221. static void o2net_rx_until_empty(struct work_struct *work)
  1222. {
  1223. struct o2net_sock_container *sc =
  1224. container_of(work, struct o2net_sock_container, sc_rx_work);
  1225. int ret;
  1226. do {
  1227. ret = o2net_advance_rx(sc);
  1228. } while (ret > 0);
  1229. if (ret <= 0 && ret != -EAGAIN) {
  1230. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  1231. sclog(sc, "saw error %d, closing\n", ret);
  1232. /* not permanent so read failed handshake can retry */
  1233. o2net_ensure_shutdown(nn, sc, 0);
  1234. }
  1235. sc_put(sc);
  1236. }
  1237. static int o2net_set_nodelay(struct socket *sock)
  1238. {
  1239. int val = 1;
  1240. return kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
  1241. (void *)&val, sizeof(val));
  1242. }
  1243. static int o2net_set_usertimeout(struct socket *sock)
  1244. {
  1245. int user_timeout = O2NET_TCP_USER_TIMEOUT;
  1246. return kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
  1247. (void *)&user_timeout, sizeof(user_timeout));
  1248. }
  1249. static void o2net_initialize_handshake(void)
  1250. {
  1251. o2net_hand->o2hb_heartbeat_timeout_ms = cpu_to_be32(
  1252. O2HB_MAX_WRITE_TIMEOUT_MS);
  1253. o2net_hand->o2net_idle_timeout_ms = cpu_to_be32(o2net_idle_timeout());
  1254. o2net_hand->o2net_keepalive_delay_ms = cpu_to_be32(
  1255. o2net_keepalive_delay());
  1256. o2net_hand->o2net_reconnect_delay_ms = cpu_to_be32(
  1257. o2net_reconnect_delay());
  1258. }
  1259. /* ------------------------------------------------------------ */
  1260. /* called when a connect completes and after a sock is accepted. the
  1261. * rx path will see the response and mark the sc valid */
  1262. static void o2net_sc_connect_completed(struct work_struct *work)
  1263. {
  1264. struct o2net_sock_container *sc =
  1265. container_of(work, struct o2net_sock_container,
  1266. sc_connect_work);
  1267. mlog(ML_MSG, "sc sending handshake with ver %llu id %llx\n",
  1268. (unsigned long long)O2NET_PROTOCOL_VERSION,
  1269. (unsigned long long)be64_to_cpu(o2net_hand->connector_id));
  1270. o2net_initialize_handshake();
  1271. o2net_sendpage(sc, o2net_hand, sizeof(*o2net_hand));
  1272. sc_put(sc);
  1273. }
  1274. /* this is called as a work_struct func. */
  1275. static void o2net_sc_send_keep_req(struct work_struct *work)
  1276. {
  1277. struct o2net_sock_container *sc =
  1278. container_of(work, struct o2net_sock_container,
  1279. sc_keepalive_work.work);
  1280. o2net_sendpage(sc, o2net_keep_req, sizeof(*o2net_keep_req));
  1281. sc_put(sc);
  1282. }
  1283. /* socket shutdown does a del_timer_sync against this as it tears down.
  1284. * we can't start this timer until we've got to the point in sc buildup
  1285. * where shutdown is going to be involved */
  1286. static void o2net_idle_timer(struct timer_list *t)
  1287. {
  1288. struct o2net_sock_container *sc = from_timer(sc, t, sc_idle_timeout);
  1289. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  1290. #ifdef CONFIG_DEBUG_FS
  1291. unsigned long msecs = ktime_to_ms(ktime_get()) -
  1292. ktime_to_ms(sc->sc_tv_timer);
  1293. #else
  1294. unsigned long msecs = o2net_idle_timeout();
  1295. #endif
  1296. printk(KERN_NOTICE "o2net: Connection to " SC_NODEF_FMT " has been "
  1297. "idle for %lu.%lu secs.\n",
  1298. SC_NODEF_ARGS(sc), msecs / 1000, msecs % 1000);
  1299. /* idle timerout happen, don't shutdown the connection, but
  1300. * make fence decision. Maybe the connection can recover before
  1301. * the decision is made.
  1302. */
  1303. atomic_set(&nn->nn_timeout, 1);
  1304. o2quo_conn_err(o2net_num_from_nn(nn));
  1305. queue_delayed_work(o2net_wq, &nn->nn_still_up,
  1306. msecs_to_jiffies(O2NET_QUORUM_DELAY_MS));
  1307. o2net_sc_reset_idle_timer(sc);
  1308. }
  1309. static void o2net_sc_reset_idle_timer(struct o2net_sock_container *sc)
  1310. {
  1311. o2net_sc_cancel_delayed_work(sc, &sc->sc_keepalive_work);
  1312. o2net_sc_queue_delayed_work(sc, &sc->sc_keepalive_work,
  1313. msecs_to_jiffies(o2net_keepalive_delay()));
  1314. o2net_set_sock_timer(sc);
  1315. mod_timer(&sc->sc_idle_timeout,
  1316. jiffies + msecs_to_jiffies(o2net_idle_timeout()));
  1317. }
  1318. static void o2net_sc_postpone_idle(struct o2net_sock_container *sc)
  1319. {
  1320. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  1321. /* clear fence decision since the connection recover from timeout*/
  1322. if (atomic_read(&nn->nn_timeout)) {
  1323. o2quo_conn_up(o2net_num_from_nn(nn));
  1324. cancel_delayed_work(&nn->nn_still_up);
  1325. atomic_set(&nn->nn_timeout, 0);
  1326. }
  1327. /* Only push out an existing timer */
  1328. if (timer_pending(&sc->sc_idle_timeout))
  1329. o2net_sc_reset_idle_timer(sc);
  1330. }
  1331. /* this work func is kicked whenever a path sets the nn state which doesn't
  1332. * have valid set. This includes seeing hb come up, losing a connection,
  1333. * having a connect attempt fail, etc. This centralizes the logic which decides
  1334. * if a connect attempt should be made or if we should give up and all future
  1335. * transmit attempts should fail */
  1336. static void o2net_start_connect(struct work_struct *work)
  1337. {
  1338. struct o2net_node *nn =
  1339. container_of(work, struct o2net_node, nn_connect_work.work);
  1340. struct o2net_sock_container *sc = NULL;
  1341. struct o2nm_node *node = NULL, *mynode = NULL;
  1342. struct socket *sock = NULL;
  1343. struct sockaddr_in myaddr = {0, }, remoteaddr = {0, };
  1344. int ret = 0, stop;
  1345. unsigned int timeout;
  1346. unsigned int noio_flag;
  1347. /*
  1348. * sock_create allocates the sock with GFP_KERNEL. We must set
  1349. * per-process flag PF_MEMALLOC_NOIO so that all allocations done
  1350. * by this process are done as if GFP_NOIO was specified. So we
  1351. * are not reentering filesystem while doing memory reclaim.
  1352. */
  1353. noio_flag = memalloc_noio_save();
  1354. /* if we're greater we initiate tx, otherwise we accept */
  1355. if (o2nm_this_node() <= o2net_num_from_nn(nn))
  1356. goto out;
  1357. /* watch for racing with tearing a node down */
  1358. node = o2nm_get_node_by_num(o2net_num_from_nn(nn));
  1359. if (node == NULL)
  1360. goto out;
  1361. mynode = o2nm_get_node_by_num(o2nm_this_node());
  1362. if (mynode == NULL)
  1363. goto out;
  1364. spin_lock(&nn->nn_lock);
  1365. /*
  1366. * see if we already have one pending or have given up.
  1367. * For nn_timeout, it is set when we close the connection
  1368. * because of the idle time out. So it means that we have
  1369. * at least connected to that node successfully once,
  1370. * now try to connect to it again.
  1371. */
  1372. timeout = atomic_read(&nn->nn_timeout);
  1373. stop = (nn->nn_sc ||
  1374. (nn->nn_persistent_error &&
  1375. (nn->nn_persistent_error != -ENOTCONN || timeout == 0)));
  1376. spin_unlock(&nn->nn_lock);
  1377. if (stop)
  1378. goto out;
  1379. nn->nn_last_connect_attempt = jiffies;
  1380. sc = sc_alloc(node);
  1381. if (sc == NULL) {
  1382. mlog(0, "couldn't allocate sc\n");
  1383. ret = -ENOMEM;
  1384. goto out;
  1385. }
  1386. ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
  1387. if (ret < 0) {
  1388. mlog(0, "can't create socket: %d\n", ret);
  1389. goto out;
  1390. }
  1391. sc->sc_sock = sock; /* freed by sc_kref_release */
  1392. sock->sk->sk_allocation = GFP_ATOMIC;
  1393. myaddr.sin_family = AF_INET;
  1394. myaddr.sin_addr.s_addr = mynode->nd_ipv4_address;
  1395. myaddr.sin_port = htons(0); /* any port */
  1396. ret = sock->ops->bind(sock, (struct sockaddr *)&myaddr,
  1397. sizeof(myaddr));
  1398. if (ret) {
  1399. mlog(ML_ERROR, "bind failed with %d at address %pI4\n",
  1400. ret, &mynode->nd_ipv4_address);
  1401. goto out;
  1402. }
  1403. ret = o2net_set_nodelay(sc->sc_sock);
  1404. if (ret) {
  1405. mlog(ML_ERROR, "setting TCP_NODELAY failed with %d\n", ret);
  1406. goto out;
  1407. }
  1408. ret = o2net_set_usertimeout(sock);
  1409. if (ret) {
  1410. mlog(ML_ERROR, "set TCP_USER_TIMEOUT failed with %d\n", ret);
  1411. goto out;
  1412. }
  1413. o2net_register_callbacks(sc->sc_sock->sk, sc);
  1414. spin_lock(&nn->nn_lock);
  1415. /* handshake completion will set nn->nn_sc_valid */
  1416. o2net_set_nn_state(nn, sc, 0, 0);
  1417. spin_unlock(&nn->nn_lock);
  1418. remoteaddr.sin_family = AF_INET;
  1419. remoteaddr.sin_addr.s_addr = node->nd_ipv4_address;
  1420. remoteaddr.sin_port = node->nd_ipv4_port;
  1421. ret = sc->sc_sock->ops->connect(sc->sc_sock,
  1422. (struct sockaddr *)&remoteaddr,
  1423. sizeof(remoteaddr),
  1424. O_NONBLOCK);
  1425. if (ret == -EINPROGRESS)
  1426. ret = 0;
  1427. out:
  1428. if (ret && sc) {
  1429. printk(KERN_NOTICE "o2net: Connect attempt to " SC_NODEF_FMT
  1430. " failed with errno %d\n", SC_NODEF_ARGS(sc), ret);
  1431. /* 0 err so that another will be queued and attempted
  1432. * from set_nn_state */
  1433. o2net_ensure_shutdown(nn, sc, 0);
  1434. }
  1435. if (sc)
  1436. sc_put(sc);
  1437. if (node)
  1438. o2nm_node_put(node);
  1439. if (mynode)
  1440. o2nm_node_put(mynode);
  1441. memalloc_noio_restore(noio_flag);
  1442. return;
  1443. }
  1444. static void o2net_connect_expired(struct work_struct *work)
  1445. {
  1446. struct o2net_node *nn =
  1447. container_of(work, struct o2net_node, nn_connect_expired.work);
  1448. spin_lock(&nn->nn_lock);
  1449. if (!nn->nn_sc_valid) {
  1450. printk(KERN_NOTICE "o2net: No connection established with "
  1451. "node %u after %u.%u seconds, check network and"
  1452. " cluster configuration.\n",
  1453. o2net_num_from_nn(nn),
  1454. o2net_idle_timeout() / 1000,
  1455. o2net_idle_timeout() % 1000);
  1456. o2net_set_nn_state(nn, NULL, 0, 0);
  1457. }
  1458. spin_unlock(&nn->nn_lock);
  1459. }
  1460. static void o2net_still_up(struct work_struct *work)
  1461. {
  1462. struct o2net_node *nn =
  1463. container_of(work, struct o2net_node, nn_still_up.work);
  1464. o2quo_hb_still_up(o2net_num_from_nn(nn));
  1465. }
  1466. /* ------------------------------------------------------------ */
  1467. void o2net_disconnect_node(struct o2nm_node *node)
  1468. {
  1469. struct o2net_node *nn = o2net_nn_from_num(node->nd_num);
  1470. /* don't reconnect until it's heartbeating again */
  1471. spin_lock(&nn->nn_lock);
  1472. atomic_set(&nn->nn_timeout, 0);
  1473. o2net_set_nn_state(nn, NULL, 0, -ENOTCONN);
  1474. spin_unlock(&nn->nn_lock);
  1475. if (o2net_wq) {
  1476. cancel_delayed_work(&nn->nn_connect_expired);
  1477. cancel_delayed_work(&nn->nn_connect_work);
  1478. cancel_delayed_work(&nn->nn_still_up);
  1479. flush_workqueue(o2net_wq);
  1480. }
  1481. }
  1482. static void o2net_hb_node_down_cb(struct o2nm_node *node, int node_num,
  1483. void *data)
  1484. {
  1485. o2quo_hb_down(node_num);
  1486. if (!node)
  1487. return;
  1488. if (node_num != o2nm_this_node())
  1489. o2net_disconnect_node(node);
  1490. BUG_ON(atomic_read(&o2net_connected_peers) < 0);
  1491. }
  1492. static void o2net_hb_node_up_cb(struct o2nm_node *node, int node_num,
  1493. void *data)
  1494. {
  1495. struct o2net_node *nn = o2net_nn_from_num(node_num);
  1496. o2quo_hb_up(node_num);
  1497. BUG_ON(!node);
  1498. /* ensure an immediate connect attempt */
  1499. nn->nn_last_connect_attempt = jiffies -
  1500. (msecs_to_jiffies(o2net_reconnect_delay()) + 1);
  1501. if (node_num != o2nm_this_node()) {
  1502. /* believe it or not, accept and node hearbeating testing
  1503. * can succeed for this node before we got here.. so
  1504. * only use set_nn_state to clear the persistent error
  1505. * if that hasn't already happened */
  1506. spin_lock(&nn->nn_lock);
  1507. atomic_set(&nn->nn_timeout, 0);
  1508. if (nn->nn_persistent_error)
  1509. o2net_set_nn_state(nn, NULL, 0, 0);
  1510. spin_unlock(&nn->nn_lock);
  1511. }
  1512. }
  1513. void o2net_unregister_hb_callbacks(void)
  1514. {
  1515. o2hb_unregister_callback(NULL, &o2net_hb_up);
  1516. o2hb_unregister_callback(NULL, &o2net_hb_down);
  1517. }
  1518. int o2net_register_hb_callbacks(void)
  1519. {
  1520. int ret;
  1521. o2hb_setup_callback(&o2net_hb_down, O2HB_NODE_DOWN_CB,
  1522. o2net_hb_node_down_cb, NULL, O2NET_HB_PRI);
  1523. o2hb_setup_callback(&o2net_hb_up, O2HB_NODE_UP_CB,
  1524. o2net_hb_node_up_cb, NULL, O2NET_HB_PRI);
  1525. ret = o2hb_register_callback(NULL, &o2net_hb_up);
  1526. if (ret == 0)
  1527. ret = o2hb_register_callback(NULL, &o2net_hb_down);
  1528. if (ret)
  1529. o2net_unregister_hb_callbacks();
  1530. return ret;
  1531. }
  1532. /* ------------------------------------------------------------ */
  1533. static int o2net_accept_one(struct socket *sock, int *more)
  1534. {
  1535. int ret;
  1536. struct sockaddr_in sin;
  1537. struct socket *new_sock = NULL;
  1538. struct o2nm_node *node = NULL;
  1539. struct o2nm_node *local_node = NULL;
  1540. struct o2net_sock_container *sc = NULL;
  1541. struct o2net_node *nn;
  1542. unsigned int noio_flag;
  1543. /*
  1544. * sock_create_lite allocates the sock with GFP_KERNEL. We must set
  1545. * per-process flag PF_MEMALLOC_NOIO so that all allocations done
  1546. * by this process are done as if GFP_NOIO was specified. So we
  1547. * are not reentering filesystem while doing memory reclaim.
  1548. */
  1549. noio_flag = memalloc_noio_save();
  1550. BUG_ON(sock == NULL);
  1551. *more = 0;
  1552. ret = sock_create_lite(sock->sk->sk_family, sock->sk->sk_type,
  1553. sock->sk->sk_protocol, &new_sock);
  1554. if (ret)
  1555. goto out;
  1556. new_sock->type = sock->type;
  1557. new_sock->ops = sock->ops;
  1558. ret = sock->ops->accept(sock, new_sock, O_NONBLOCK, false);
  1559. if (ret < 0)
  1560. goto out;
  1561. *more = 1;
  1562. new_sock->sk->sk_allocation = GFP_ATOMIC;
  1563. ret = o2net_set_nodelay(new_sock);
  1564. if (ret) {
  1565. mlog(ML_ERROR, "setting TCP_NODELAY failed with %d\n", ret);
  1566. goto out;
  1567. }
  1568. ret = o2net_set_usertimeout(new_sock);
  1569. if (ret) {
  1570. mlog(ML_ERROR, "set TCP_USER_TIMEOUT failed with %d\n", ret);
  1571. goto out;
  1572. }
  1573. ret = new_sock->ops->getname(new_sock, (struct sockaddr *) &sin, 1);
  1574. if (ret < 0)
  1575. goto out;
  1576. node = o2nm_get_node_by_ip(sin.sin_addr.s_addr);
  1577. if (node == NULL) {
  1578. printk(KERN_NOTICE "o2net: Attempt to connect from unknown "
  1579. "node at %pI4:%d\n", &sin.sin_addr.s_addr,
  1580. ntohs(sin.sin_port));
  1581. ret = -EINVAL;
  1582. goto out;
  1583. }
  1584. if (o2nm_this_node() >= node->nd_num) {
  1585. local_node = o2nm_get_node_by_num(o2nm_this_node());
  1586. if (local_node)
  1587. printk(KERN_NOTICE "o2net: Unexpected connect attempt "
  1588. "seen at node '%s' (%u, %pI4:%d) from "
  1589. "node '%s' (%u, %pI4:%d)\n",
  1590. local_node->nd_name, local_node->nd_num,
  1591. &(local_node->nd_ipv4_address),
  1592. ntohs(local_node->nd_ipv4_port),
  1593. node->nd_name,
  1594. node->nd_num, &sin.sin_addr.s_addr,
  1595. ntohs(sin.sin_port));
  1596. ret = -EINVAL;
  1597. goto out;
  1598. }
  1599. /* this happens all the time when the other node sees our heartbeat
  1600. * and tries to connect before we see their heartbeat */
  1601. if (!o2hb_check_node_heartbeating_from_callback(node->nd_num)) {
  1602. mlog(ML_CONN, "attempt to connect from node '%s' at "
  1603. "%pI4:%d but it isn't heartbeating\n",
  1604. node->nd_name, &sin.sin_addr.s_addr,
  1605. ntohs(sin.sin_port));
  1606. ret = -EINVAL;
  1607. goto out;
  1608. }
  1609. nn = o2net_nn_from_num(node->nd_num);
  1610. spin_lock(&nn->nn_lock);
  1611. if (nn->nn_sc)
  1612. ret = -EBUSY;
  1613. else
  1614. ret = 0;
  1615. spin_unlock(&nn->nn_lock);
  1616. if (ret) {
  1617. printk(KERN_NOTICE "o2net: Attempt to connect from node '%s' "
  1618. "at %pI4:%d but it already has an open connection\n",
  1619. node->nd_name, &sin.sin_addr.s_addr,
  1620. ntohs(sin.sin_port));
  1621. goto out;
  1622. }
  1623. sc = sc_alloc(node);
  1624. if (sc == NULL) {
  1625. ret = -ENOMEM;
  1626. goto out;
  1627. }
  1628. sc->sc_sock = new_sock;
  1629. new_sock = NULL;
  1630. spin_lock(&nn->nn_lock);
  1631. atomic_set(&nn->nn_timeout, 0);
  1632. o2net_set_nn_state(nn, sc, 0, 0);
  1633. spin_unlock(&nn->nn_lock);
  1634. o2net_register_callbacks(sc->sc_sock->sk, sc);
  1635. o2net_sc_queue_work(sc, &sc->sc_rx_work);
  1636. o2net_initialize_handshake();
  1637. o2net_sendpage(sc, o2net_hand, sizeof(*o2net_hand));
  1638. out:
  1639. if (new_sock)
  1640. sock_release(new_sock);
  1641. if (node)
  1642. o2nm_node_put(node);
  1643. if (local_node)
  1644. o2nm_node_put(local_node);
  1645. if (sc)
  1646. sc_put(sc);
  1647. memalloc_noio_restore(noio_flag);
  1648. return ret;
  1649. }
  1650. /*
  1651. * This function is invoked in response to one or more
  1652. * pending accepts at softIRQ level. We must drain the
  1653. * entire que before returning.
  1654. */
  1655. static void o2net_accept_many(struct work_struct *work)
  1656. {
  1657. struct socket *sock = o2net_listen_sock;
  1658. int more;
  1659. int err;
  1660. /*
  1661. * It is critical to note that due to interrupt moderation
  1662. * at the network driver level, we can't assume to get a
  1663. * softIRQ for every single conn since tcp SYN packets
  1664. * can arrive back-to-back, and therefore many pending
  1665. * accepts may result in just 1 softIRQ. If we terminate
  1666. * the o2net_accept_one() loop upon seeing an err, what happens
  1667. * to the rest of the conns in the queue? If no new SYN
  1668. * arrives for hours, no softIRQ will be delivered,
  1669. * and the connections will just sit in the queue.
  1670. */
  1671. for (;;) {
  1672. err = o2net_accept_one(sock, &more);
  1673. if (!more)
  1674. break;
  1675. cond_resched();
  1676. }
  1677. }
  1678. static void o2net_listen_data_ready(struct sock *sk)
  1679. {
  1680. void (*ready)(struct sock *sk);
  1681. read_lock_bh(&sk->sk_callback_lock);
  1682. ready = sk->sk_user_data;
  1683. if (ready == NULL) { /* check for teardown race */
  1684. ready = sk->sk_data_ready;
  1685. goto out;
  1686. }
  1687. /* This callback may called twice when a new connection
  1688. * is being established as a child socket inherits everything
  1689. * from a parent LISTEN socket, including the data_ready cb of
  1690. * the parent. This leads to a hazard. In o2net_accept_one()
  1691. * we are still initializing the child socket but have not
  1692. * changed the inherited data_ready callback yet when
  1693. * data starts arriving.
  1694. * We avoid this hazard by checking the state.
  1695. * For the listening socket, the state will be TCP_LISTEN; for the new
  1696. * socket, will be TCP_ESTABLISHED. Also, in this case,
  1697. * sk->sk_user_data is not a valid function pointer.
  1698. */
  1699. if (sk->sk_state == TCP_LISTEN) {
  1700. queue_work(o2net_wq, &o2net_listen_work);
  1701. } else {
  1702. ready = NULL;
  1703. }
  1704. out:
  1705. read_unlock_bh(&sk->sk_callback_lock);
  1706. if (ready != NULL)
  1707. ready(sk);
  1708. }
  1709. static int o2net_open_listening_sock(__be32 addr, __be16 port)
  1710. {
  1711. struct socket *sock = NULL;
  1712. int ret;
  1713. struct sockaddr_in sin = {
  1714. .sin_family = PF_INET,
  1715. .sin_addr = { .s_addr = addr },
  1716. .sin_port = port,
  1717. };
  1718. ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
  1719. if (ret < 0) {
  1720. printk(KERN_ERR "o2net: Error %d while creating socket\n", ret);
  1721. goto out;
  1722. }
  1723. sock->sk->sk_allocation = GFP_ATOMIC;
  1724. write_lock_bh(&sock->sk->sk_callback_lock);
  1725. sock->sk->sk_user_data = sock->sk->sk_data_ready;
  1726. sock->sk->sk_data_ready = o2net_listen_data_ready;
  1727. write_unlock_bh(&sock->sk->sk_callback_lock);
  1728. o2net_listen_sock = sock;
  1729. INIT_WORK(&o2net_listen_work, o2net_accept_many);
  1730. sock->sk->sk_reuse = SK_CAN_REUSE;
  1731. ret = sock->ops->bind(sock, (struct sockaddr *)&sin, sizeof(sin));
  1732. if (ret < 0) {
  1733. printk(KERN_ERR "o2net: Error %d while binding socket at "
  1734. "%pI4:%u\n", ret, &addr, ntohs(port));
  1735. goto out;
  1736. }
  1737. ret = sock->ops->listen(sock, 64);
  1738. if (ret < 0)
  1739. printk(KERN_ERR "o2net: Error %d while listening on %pI4:%u\n",
  1740. ret, &addr, ntohs(port));
  1741. out:
  1742. if (ret) {
  1743. o2net_listen_sock = NULL;
  1744. if (sock)
  1745. sock_release(sock);
  1746. }
  1747. return ret;
  1748. }
  1749. /*
  1750. * called from node manager when we should bring up our network listening
  1751. * socket. node manager handles all the serialization to only call this
  1752. * once and to match it with o2net_stop_listening(). note,
  1753. * o2nm_this_node() doesn't work yet as we're being called while it
  1754. * is being set up.
  1755. */
  1756. int o2net_start_listening(struct o2nm_node *node)
  1757. {
  1758. int ret = 0;
  1759. BUG_ON(o2net_wq != NULL);
  1760. BUG_ON(o2net_listen_sock != NULL);
  1761. mlog(ML_KTHREAD, "starting o2net thread...\n");
  1762. o2net_wq = alloc_ordered_workqueue("o2net", WQ_MEM_RECLAIM);
  1763. if (o2net_wq == NULL) {
  1764. mlog(ML_ERROR, "unable to launch o2net thread\n");
  1765. return -ENOMEM; /* ? */
  1766. }
  1767. ret = o2net_open_listening_sock(node->nd_ipv4_address,
  1768. node->nd_ipv4_port);
  1769. if (ret) {
  1770. destroy_workqueue(o2net_wq);
  1771. o2net_wq = NULL;
  1772. } else
  1773. o2quo_conn_up(node->nd_num);
  1774. return ret;
  1775. }
  1776. /* again, o2nm_this_node() doesn't work here as we're involved in
  1777. * tearing it down */
  1778. void o2net_stop_listening(struct o2nm_node *node)
  1779. {
  1780. struct socket *sock = o2net_listen_sock;
  1781. size_t i;
  1782. BUG_ON(o2net_wq == NULL);
  1783. BUG_ON(o2net_listen_sock == NULL);
  1784. /* stop the listening socket from generating work */
  1785. write_lock_bh(&sock->sk->sk_callback_lock);
  1786. sock->sk->sk_data_ready = sock->sk->sk_user_data;
  1787. sock->sk->sk_user_data = NULL;
  1788. write_unlock_bh(&sock->sk->sk_callback_lock);
  1789. for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) {
  1790. struct o2nm_node *node = o2nm_get_node_by_num(i);
  1791. if (node) {
  1792. o2net_disconnect_node(node);
  1793. o2nm_node_put(node);
  1794. }
  1795. }
  1796. /* finish all work and tear down the work queue */
  1797. mlog(ML_KTHREAD, "waiting for o2net thread to exit....\n");
  1798. destroy_workqueue(o2net_wq);
  1799. o2net_wq = NULL;
  1800. sock_release(o2net_listen_sock);
  1801. o2net_listen_sock = NULL;
  1802. o2quo_conn_err(node->nd_num);
  1803. }
  1804. /* ------------------------------------------------------------ */
  1805. int o2net_init(void)
  1806. {
  1807. unsigned long i;
  1808. o2quo_init();
  1809. if (o2net_debugfs_init())
  1810. goto out;
  1811. o2net_hand = kzalloc(sizeof(struct o2net_handshake), GFP_KERNEL);
  1812. o2net_keep_req = kzalloc(sizeof(struct o2net_msg), GFP_KERNEL);
  1813. o2net_keep_resp = kzalloc(sizeof(struct o2net_msg), GFP_KERNEL);
  1814. if (!o2net_hand || !o2net_keep_req || !o2net_keep_resp)
  1815. goto out;
  1816. o2net_hand->protocol_version = cpu_to_be64(O2NET_PROTOCOL_VERSION);
  1817. o2net_hand->connector_id = cpu_to_be64(1);
  1818. o2net_keep_req->magic = cpu_to_be16(O2NET_MSG_KEEP_REQ_MAGIC);
  1819. o2net_keep_resp->magic = cpu_to_be16(O2NET_MSG_KEEP_RESP_MAGIC);
  1820. for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) {
  1821. struct o2net_node *nn = o2net_nn_from_num(i);
  1822. atomic_set(&nn->nn_timeout, 0);
  1823. spin_lock_init(&nn->nn_lock);
  1824. INIT_DELAYED_WORK(&nn->nn_connect_work, o2net_start_connect);
  1825. INIT_DELAYED_WORK(&nn->nn_connect_expired,
  1826. o2net_connect_expired);
  1827. INIT_DELAYED_WORK(&nn->nn_still_up, o2net_still_up);
  1828. /* until we see hb from a node we'll return einval */
  1829. nn->nn_persistent_error = -ENOTCONN;
  1830. init_waitqueue_head(&nn->nn_sc_wq);
  1831. idr_init(&nn->nn_status_idr);
  1832. INIT_LIST_HEAD(&nn->nn_status_list);
  1833. }
  1834. return 0;
  1835. out:
  1836. kfree(o2net_hand);
  1837. kfree(o2net_keep_req);
  1838. kfree(o2net_keep_resp);
  1839. o2net_debugfs_exit();
  1840. o2quo_exit();
  1841. return -ENOMEM;
  1842. }
  1843. void o2net_exit(void)
  1844. {
  1845. o2quo_exit();
  1846. kfree(o2net_hand);
  1847. kfree(o2net_keep_req);
  1848. kfree(o2net_keep_resp);
  1849. o2net_debugfs_exit();
  1850. }