tcp.c 54 KB

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