tcp.c 57 KB

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