tcp.c 50 KB

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