tcp.c 56 KB

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