lowcomms.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804
  1. /******************************************************************************
  2. *******************************************************************************
  3. **
  4. ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  5. ** Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
  6. **
  7. ** This copyrighted material is made available to anyone wishing to use,
  8. ** modify, copy, or redistribute it subject to the terms and conditions
  9. ** of the GNU General Public License v.2.
  10. **
  11. *******************************************************************************
  12. ******************************************************************************/
  13. /*
  14. * lowcomms.c
  15. *
  16. * This is the "low-level" comms layer.
  17. *
  18. * It is responsible for sending/receiving messages
  19. * from other nodes in the cluster.
  20. *
  21. * Cluster nodes are referred to by their nodeids. nodeids are
  22. * simply 32 bit numbers to the locking module - if they need to
  23. * be expanded for the cluster infrastructure then that is its
  24. * responsibility. It is this layer's
  25. * responsibility to resolve these into IP address or
  26. * whatever it needs for inter-node communication.
  27. *
  28. * The comms level is two kernel threads that deal mainly with
  29. * the receiving of messages from other nodes and passing them
  30. * up to the mid-level comms layer (which understands the
  31. * message format) for execution by the locking core, and
  32. * a send thread which does all the setting up of connections
  33. * to remote nodes and the sending of data. Threads are not allowed
  34. * to send their own data because it may cause them to wait in times
  35. * of high load. Also, this way, the sending thread can collect together
  36. * messages bound for one node and send them in one block.
  37. *
  38. * lowcomms will choose to use either TCP or SCTP as its transport layer
  39. * depending on the configuration variable 'protocol'. This should be set
  40. * to 0 (default) for TCP or 1 for SCTP. It should be configured using a
  41. * cluster-wide mechanism as it must be the same on all nodes of the cluster
  42. * for the DLM to function.
  43. *
  44. */
  45. #include <asm/ioctls.h>
  46. #include <net/sock.h>
  47. #include <net/tcp.h>
  48. #include <linux/pagemap.h>
  49. #include <linux/file.h>
  50. #include <linux/mutex.h>
  51. #include <linux/sctp.h>
  52. #include <linux/slab.h>
  53. #include <net/sctp/sctp.h>
  54. #include <net/ipv6.h>
  55. #include "dlm_internal.h"
  56. #include "lowcomms.h"
  57. #include "midcomms.h"
  58. #include "config.h"
  59. #define NEEDED_RMEM (4*1024*1024)
  60. #define CONN_HASH_SIZE 32
  61. /* Number of messages to send before rescheduling */
  62. #define MAX_SEND_MSG_COUNT 25
  63. struct cbuf {
  64. unsigned int base;
  65. unsigned int len;
  66. unsigned int mask;
  67. };
  68. static void cbuf_add(struct cbuf *cb, int n)
  69. {
  70. cb->len += n;
  71. }
  72. static int cbuf_data(struct cbuf *cb)
  73. {
  74. return ((cb->base + cb->len) & cb->mask);
  75. }
  76. static void cbuf_init(struct cbuf *cb, int size)
  77. {
  78. cb->base = cb->len = 0;
  79. cb->mask = size-1;
  80. }
  81. static void cbuf_eat(struct cbuf *cb, int n)
  82. {
  83. cb->len -= n;
  84. cb->base += n;
  85. cb->base &= cb->mask;
  86. }
  87. static bool cbuf_empty(struct cbuf *cb)
  88. {
  89. return cb->len == 0;
  90. }
  91. struct connection {
  92. struct socket *sock; /* NULL if not connected */
  93. uint32_t nodeid; /* So we know who we are in the list */
  94. struct mutex sock_mutex;
  95. unsigned long flags;
  96. #define CF_READ_PENDING 1
  97. #define CF_WRITE_PENDING 2
  98. #define CF_INIT_PENDING 4
  99. #define CF_IS_OTHERCON 5
  100. #define CF_CLOSE 6
  101. #define CF_APP_LIMITED 7
  102. #define CF_CLOSING 8
  103. struct list_head writequeue; /* List of outgoing writequeue_entries */
  104. spinlock_t writequeue_lock;
  105. int (*rx_action) (struct connection *); /* What to do when active */
  106. void (*connect_action) (struct connection *); /* What to do to connect */
  107. struct page *rx_page;
  108. struct cbuf cb;
  109. int retries;
  110. #define MAX_CONNECT_RETRIES 3
  111. struct hlist_node list;
  112. struct connection *othercon;
  113. struct work_struct rwork; /* Receive workqueue */
  114. struct work_struct swork; /* Send workqueue */
  115. };
  116. #define sock2con(x) ((struct connection *)(x)->sk_user_data)
  117. /* An entry waiting to be sent */
  118. struct writequeue_entry {
  119. struct list_head list;
  120. struct page *page;
  121. int offset;
  122. int len;
  123. int end;
  124. int users;
  125. struct connection *con;
  126. };
  127. struct dlm_node_addr {
  128. struct list_head list;
  129. int nodeid;
  130. int addr_count;
  131. int curr_addr_index;
  132. struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
  133. };
  134. static struct listen_sock_callbacks {
  135. void (*sk_error_report)(struct sock *);
  136. void (*sk_data_ready)(struct sock *);
  137. void (*sk_state_change)(struct sock *);
  138. void (*sk_write_space)(struct sock *);
  139. } listen_sock;
  140. static LIST_HEAD(dlm_node_addrs);
  141. static DEFINE_SPINLOCK(dlm_node_addrs_spin);
  142. static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
  143. static int dlm_local_count;
  144. static int dlm_allow_conn;
  145. /* Work queues */
  146. static struct workqueue_struct *recv_workqueue;
  147. static struct workqueue_struct *send_workqueue;
  148. static struct hlist_head connection_hash[CONN_HASH_SIZE];
  149. static DEFINE_MUTEX(connections_lock);
  150. static struct kmem_cache *con_cache;
  151. static void process_recv_sockets(struct work_struct *work);
  152. static void process_send_sockets(struct work_struct *work);
  153. /* This is deliberately very simple because most clusters have simple
  154. sequential nodeids, so we should be able to go straight to a connection
  155. struct in the array */
  156. static inline int nodeid_hash(int nodeid)
  157. {
  158. return nodeid & (CONN_HASH_SIZE-1);
  159. }
  160. static struct connection *__find_con(int nodeid)
  161. {
  162. int r;
  163. struct connection *con;
  164. r = nodeid_hash(nodeid);
  165. hlist_for_each_entry(con, &connection_hash[r], list) {
  166. if (con->nodeid == nodeid)
  167. return con;
  168. }
  169. return NULL;
  170. }
  171. /*
  172. * If 'allocation' is zero then we don't attempt to create a new
  173. * connection structure for this node.
  174. */
  175. static struct connection *__nodeid2con(int nodeid, gfp_t alloc)
  176. {
  177. struct connection *con = NULL;
  178. int r;
  179. con = __find_con(nodeid);
  180. if (con || !alloc)
  181. return con;
  182. con = kmem_cache_zalloc(con_cache, alloc);
  183. if (!con)
  184. return NULL;
  185. r = nodeid_hash(nodeid);
  186. hlist_add_head(&con->list, &connection_hash[r]);
  187. con->nodeid = nodeid;
  188. mutex_init(&con->sock_mutex);
  189. INIT_LIST_HEAD(&con->writequeue);
  190. spin_lock_init(&con->writequeue_lock);
  191. INIT_WORK(&con->swork, process_send_sockets);
  192. INIT_WORK(&con->rwork, process_recv_sockets);
  193. /* Setup action pointers for child sockets */
  194. if (con->nodeid) {
  195. struct connection *zerocon = __find_con(0);
  196. con->connect_action = zerocon->connect_action;
  197. if (!con->rx_action)
  198. con->rx_action = zerocon->rx_action;
  199. }
  200. return con;
  201. }
  202. /* Loop round all connections */
  203. static void foreach_conn(void (*conn_func)(struct connection *c))
  204. {
  205. int i;
  206. struct hlist_node *n;
  207. struct connection *con;
  208. for (i = 0; i < CONN_HASH_SIZE; i++) {
  209. hlist_for_each_entry_safe(con, n, &connection_hash[i], list)
  210. conn_func(con);
  211. }
  212. }
  213. static struct connection *nodeid2con(int nodeid, gfp_t allocation)
  214. {
  215. struct connection *con;
  216. mutex_lock(&connections_lock);
  217. con = __nodeid2con(nodeid, allocation);
  218. mutex_unlock(&connections_lock);
  219. return con;
  220. }
  221. static struct dlm_node_addr *find_node_addr(int nodeid)
  222. {
  223. struct dlm_node_addr *na;
  224. list_for_each_entry(na, &dlm_node_addrs, list) {
  225. if (na->nodeid == nodeid)
  226. return na;
  227. }
  228. return NULL;
  229. }
  230. static int addr_compare(struct sockaddr_storage *x, struct sockaddr_storage *y)
  231. {
  232. switch (x->ss_family) {
  233. case AF_INET: {
  234. struct sockaddr_in *sinx = (struct sockaddr_in *)x;
  235. struct sockaddr_in *siny = (struct sockaddr_in *)y;
  236. if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
  237. return 0;
  238. if (sinx->sin_port != siny->sin_port)
  239. return 0;
  240. break;
  241. }
  242. case AF_INET6: {
  243. struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
  244. struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
  245. if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
  246. return 0;
  247. if (sinx->sin6_port != siny->sin6_port)
  248. return 0;
  249. break;
  250. }
  251. default:
  252. return 0;
  253. }
  254. return 1;
  255. }
  256. static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
  257. struct sockaddr *sa_out, bool try_new_addr)
  258. {
  259. struct sockaddr_storage sas;
  260. struct dlm_node_addr *na;
  261. if (!dlm_local_count)
  262. return -1;
  263. spin_lock(&dlm_node_addrs_spin);
  264. na = find_node_addr(nodeid);
  265. if (na && na->addr_count) {
  266. memcpy(&sas, na->addr[na->curr_addr_index],
  267. sizeof(struct sockaddr_storage));
  268. if (try_new_addr) {
  269. na->curr_addr_index++;
  270. if (na->curr_addr_index == na->addr_count)
  271. na->curr_addr_index = 0;
  272. }
  273. }
  274. spin_unlock(&dlm_node_addrs_spin);
  275. if (!na)
  276. return -EEXIST;
  277. if (!na->addr_count)
  278. return -ENOENT;
  279. if (sas_out)
  280. memcpy(sas_out, &sas, sizeof(struct sockaddr_storage));
  281. if (!sa_out)
  282. return 0;
  283. if (dlm_local_addr[0]->ss_family == AF_INET) {
  284. struct sockaddr_in *in4 = (struct sockaddr_in *) &sas;
  285. struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out;
  286. ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
  287. } else {
  288. struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas;
  289. struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out;
  290. ret6->sin6_addr = in6->sin6_addr;
  291. }
  292. return 0;
  293. }
  294. static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid)
  295. {
  296. struct dlm_node_addr *na;
  297. int rv = -EEXIST;
  298. int addr_i;
  299. spin_lock(&dlm_node_addrs_spin);
  300. list_for_each_entry(na, &dlm_node_addrs, list) {
  301. if (!na->addr_count)
  302. continue;
  303. for (addr_i = 0; addr_i < na->addr_count; addr_i++) {
  304. if (addr_compare(na->addr[addr_i], addr)) {
  305. *nodeid = na->nodeid;
  306. rv = 0;
  307. goto unlock;
  308. }
  309. }
  310. }
  311. unlock:
  312. spin_unlock(&dlm_node_addrs_spin);
  313. return rv;
  314. }
  315. int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len)
  316. {
  317. struct sockaddr_storage *new_addr;
  318. struct dlm_node_addr *new_node, *na;
  319. new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS);
  320. if (!new_node)
  321. return -ENOMEM;
  322. new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS);
  323. if (!new_addr) {
  324. kfree(new_node);
  325. return -ENOMEM;
  326. }
  327. memcpy(new_addr, addr, len);
  328. spin_lock(&dlm_node_addrs_spin);
  329. na = find_node_addr(nodeid);
  330. if (!na) {
  331. new_node->nodeid = nodeid;
  332. new_node->addr[0] = new_addr;
  333. new_node->addr_count = 1;
  334. list_add(&new_node->list, &dlm_node_addrs);
  335. spin_unlock(&dlm_node_addrs_spin);
  336. return 0;
  337. }
  338. if (na->addr_count >= DLM_MAX_ADDR_COUNT) {
  339. spin_unlock(&dlm_node_addrs_spin);
  340. kfree(new_addr);
  341. kfree(new_node);
  342. return -ENOSPC;
  343. }
  344. na->addr[na->addr_count++] = new_addr;
  345. spin_unlock(&dlm_node_addrs_spin);
  346. kfree(new_node);
  347. return 0;
  348. }
  349. /* Data available on socket or listen socket received a connect */
  350. static void lowcomms_data_ready(struct sock *sk)
  351. {
  352. struct connection *con;
  353. read_lock_bh(&sk->sk_callback_lock);
  354. con = sock2con(sk);
  355. if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
  356. queue_work(recv_workqueue, &con->rwork);
  357. read_unlock_bh(&sk->sk_callback_lock);
  358. }
  359. static void lowcomms_write_space(struct sock *sk)
  360. {
  361. struct connection *con;
  362. read_lock_bh(&sk->sk_callback_lock);
  363. con = sock2con(sk);
  364. if (!con)
  365. goto out;
  366. clear_bit(SOCK_NOSPACE, &con->sock->flags);
  367. if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
  368. con->sock->sk->sk_write_pending--;
  369. clear_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags);
  370. }
  371. queue_work(send_workqueue, &con->swork);
  372. out:
  373. read_unlock_bh(&sk->sk_callback_lock);
  374. }
  375. static inline void lowcomms_connect_sock(struct connection *con)
  376. {
  377. if (test_bit(CF_CLOSE, &con->flags))
  378. return;
  379. queue_work(send_workqueue, &con->swork);
  380. cond_resched();
  381. }
  382. static void lowcomms_state_change(struct sock *sk)
  383. {
  384. /* SCTP layer is not calling sk_data_ready when the connection
  385. * is done, so we catch the signal through here. Also, it
  386. * doesn't switch socket state when entering shutdown, so we
  387. * skip the write in that case.
  388. */
  389. if (sk->sk_shutdown) {
  390. if (sk->sk_shutdown == RCV_SHUTDOWN)
  391. lowcomms_data_ready(sk);
  392. } else if (sk->sk_state == TCP_ESTABLISHED) {
  393. lowcomms_write_space(sk);
  394. }
  395. }
  396. int dlm_lowcomms_connect_node(int nodeid)
  397. {
  398. struct connection *con;
  399. if (nodeid == dlm_our_nodeid())
  400. return 0;
  401. con = nodeid2con(nodeid, GFP_NOFS);
  402. if (!con)
  403. return -ENOMEM;
  404. lowcomms_connect_sock(con);
  405. return 0;
  406. }
  407. static void lowcomms_error_report(struct sock *sk)
  408. {
  409. struct connection *con;
  410. struct sockaddr_storage saddr;
  411. void (*orig_report)(struct sock *) = NULL;
  412. read_lock_bh(&sk->sk_callback_lock);
  413. con = sock2con(sk);
  414. if (con == NULL)
  415. goto out;
  416. orig_report = listen_sock.sk_error_report;
  417. if (con->sock == NULL ||
  418. kernel_getpeername(con->sock, (struct sockaddr *)&saddr) < 0) {
  419. printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
  420. "sending to node %d, port %d, "
  421. "sk_err=%d/%d\n", dlm_our_nodeid(),
  422. con->nodeid, dlm_config.ci_tcp_port,
  423. sk->sk_err, sk->sk_err_soft);
  424. } else if (saddr.ss_family == AF_INET) {
  425. struct sockaddr_in *sin4 = (struct sockaddr_in *)&saddr;
  426. printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
  427. "sending to node %d at %pI4, port %d, "
  428. "sk_err=%d/%d\n", dlm_our_nodeid(),
  429. con->nodeid, &sin4->sin_addr.s_addr,
  430. dlm_config.ci_tcp_port, sk->sk_err,
  431. sk->sk_err_soft);
  432. } else {
  433. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&saddr;
  434. printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
  435. "sending to node %d at %u.%u.%u.%u, "
  436. "port %d, sk_err=%d/%d\n", dlm_our_nodeid(),
  437. con->nodeid, sin6->sin6_addr.s6_addr32[0],
  438. sin6->sin6_addr.s6_addr32[1],
  439. sin6->sin6_addr.s6_addr32[2],
  440. sin6->sin6_addr.s6_addr32[3],
  441. dlm_config.ci_tcp_port, sk->sk_err,
  442. sk->sk_err_soft);
  443. }
  444. out:
  445. read_unlock_bh(&sk->sk_callback_lock);
  446. if (orig_report)
  447. orig_report(sk);
  448. }
  449. /* Note: sk_callback_lock must be locked before calling this function. */
  450. static void save_listen_callbacks(struct socket *sock)
  451. {
  452. struct sock *sk = sock->sk;
  453. listen_sock.sk_data_ready = sk->sk_data_ready;
  454. listen_sock.sk_state_change = sk->sk_state_change;
  455. listen_sock.sk_write_space = sk->sk_write_space;
  456. listen_sock.sk_error_report = sk->sk_error_report;
  457. }
  458. static void restore_callbacks(struct socket *sock)
  459. {
  460. struct sock *sk = sock->sk;
  461. write_lock_bh(&sk->sk_callback_lock);
  462. sk->sk_user_data = NULL;
  463. sk->sk_data_ready = listen_sock.sk_data_ready;
  464. sk->sk_state_change = listen_sock.sk_state_change;
  465. sk->sk_write_space = listen_sock.sk_write_space;
  466. sk->sk_error_report = listen_sock.sk_error_report;
  467. write_unlock_bh(&sk->sk_callback_lock);
  468. }
  469. /* Make a socket active */
  470. static void add_sock(struct socket *sock, struct connection *con)
  471. {
  472. struct sock *sk = sock->sk;
  473. write_lock_bh(&sk->sk_callback_lock);
  474. con->sock = sock;
  475. sk->sk_user_data = con;
  476. /* Install a data_ready callback */
  477. sk->sk_data_ready = lowcomms_data_ready;
  478. sk->sk_write_space = lowcomms_write_space;
  479. sk->sk_state_change = lowcomms_state_change;
  480. sk->sk_allocation = GFP_NOFS;
  481. sk->sk_error_report = lowcomms_error_report;
  482. write_unlock_bh(&sk->sk_callback_lock);
  483. }
  484. /* Add the port number to an IPv6 or 4 sockaddr and return the address
  485. length */
  486. static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
  487. int *addr_len)
  488. {
  489. saddr->ss_family = dlm_local_addr[0]->ss_family;
  490. if (saddr->ss_family == AF_INET) {
  491. struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
  492. in4_addr->sin_port = cpu_to_be16(port);
  493. *addr_len = sizeof(struct sockaddr_in);
  494. memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
  495. } else {
  496. struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
  497. in6_addr->sin6_port = cpu_to_be16(port);
  498. *addr_len = sizeof(struct sockaddr_in6);
  499. }
  500. memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
  501. }
  502. /* Close a remote connection and tidy up */
  503. static void close_connection(struct connection *con, bool and_other,
  504. bool tx, bool rx)
  505. {
  506. bool closing = test_and_set_bit(CF_CLOSING, &con->flags);
  507. if (tx && !closing && cancel_work_sync(&con->swork)) {
  508. log_print("canceled swork for node %d", con->nodeid);
  509. clear_bit(CF_WRITE_PENDING, &con->flags);
  510. }
  511. if (rx && !closing && cancel_work_sync(&con->rwork)) {
  512. log_print("canceled rwork for node %d", con->nodeid);
  513. clear_bit(CF_READ_PENDING, &con->flags);
  514. }
  515. mutex_lock(&con->sock_mutex);
  516. if (con->sock) {
  517. restore_callbacks(con->sock);
  518. sock_release(con->sock);
  519. con->sock = NULL;
  520. }
  521. if (con->othercon && and_other) {
  522. /* Will only re-enter once. */
  523. close_connection(con->othercon, false, true, true);
  524. }
  525. if (con->rx_page) {
  526. __free_page(con->rx_page);
  527. con->rx_page = NULL;
  528. }
  529. con->retries = 0;
  530. mutex_unlock(&con->sock_mutex);
  531. clear_bit(CF_CLOSING, &con->flags);
  532. }
  533. /* Data received from remote end */
  534. static int receive_from_sock(struct connection *con)
  535. {
  536. int ret = 0;
  537. struct msghdr msg = {};
  538. struct kvec iov[2];
  539. unsigned len;
  540. int r;
  541. int call_again_soon = 0;
  542. int nvec;
  543. mutex_lock(&con->sock_mutex);
  544. if (con->sock == NULL) {
  545. ret = -EAGAIN;
  546. goto out_close;
  547. }
  548. if (con->nodeid == 0) {
  549. ret = -EINVAL;
  550. goto out_close;
  551. }
  552. if (con->rx_page == NULL) {
  553. /*
  554. * This doesn't need to be atomic, but I think it should
  555. * improve performance if it is.
  556. */
  557. con->rx_page = alloc_page(GFP_ATOMIC);
  558. if (con->rx_page == NULL)
  559. goto out_resched;
  560. cbuf_init(&con->cb, PAGE_SIZE);
  561. }
  562. /*
  563. * iov[0] is the bit of the circular buffer between the current end
  564. * point (cb.base + cb.len) and the end of the buffer.
  565. */
  566. iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
  567. iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
  568. iov[1].iov_len = 0;
  569. nvec = 1;
  570. /*
  571. * iov[1] is the bit of the circular buffer between the start of the
  572. * buffer and the start of the currently used section (cb.base)
  573. */
  574. if (cbuf_data(&con->cb) >= con->cb.base) {
  575. iov[0].iov_len = PAGE_SIZE - cbuf_data(&con->cb);
  576. iov[1].iov_len = con->cb.base;
  577. iov[1].iov_base = page_address(con->rx_page);
  578. nvec = 2;
  579. }
  580. len = iov[0].iov_len + iov[1].iov_len;
  581. iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, iov, nvec, len);
  582. r = ret = sock_recvmsg(con->sock, &msg, MSG_DONTWAIT | MSG_NOSIGNAL);
  583. if (ret <= 0)
  584. goto out_close;
  585. else if (ret == len)
  586. call_again_soon = 1;
  587. cbuf_add(&con->cb, ret);
  588. ret = dlm_process_incoming_buffer(con->nodeid,
  589. page_address(con->rx_page),
  590. con->cb.base, con->cb.len,
  591. PAGE_SIZE);
  592. if (ret == -EBADMSG) {
  593. log_print("lowcomms: addr=%p, base=%u, len=%u, read=%d",
  594. page_address(con->rx_page), con->cb.base,
  595. con->cb.len, r);
  596. }
  597. if (ret < 0)
  598. goto out_close;
  599. cbuf_eat(&con->cb, ret);
  600. if (cbuf_empty(&con->cb) && !call_again_soon) {
  601. __free_page(con->rx_page);
  602. con->rx_page = NULL;
  603. }
  604. if (call_again_soon)
  605. goto out_resched;
  606. mutex_unlock(&con->sock_mutex);
  607. return 0;
  608. out_resched:
  609. if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
  610. queue_work(recv_workqueue, &con->rwork);
  611. mutex_unlock(&con->sock_mutex);
  612. return -EAGAIN;
  613. out_close:
  614. mutex_unlock(&con->sock_mutex);
  615. if (ret != -EAGAIN) {
  616. close_connection(con, true, true, false);
  617. /* Reconnect when there is something to send */
  618. }
  619. /* Don't return success if we really got EOF */
  620. if (ret == 0)
  621. ret = -EAGAIN;
  622. return ret;
  623. }
  624. /* Listening socket is busy, accept a connection */
  625. static int tcp_accept_from_sock(struct connection *con)
  626. {
  627. int result;
  628. struct sockaddr_storage peeraddr;
  629. struct socket *newsock;
  630. int len;
  631. int nodeid;
  632. struct connection *newcon;
  633. struct connection *addcon;
  634. mutex_lock(&connections_lock);
  635. if (!dlm_allow_conn) {
  636. mutex_unlock(&connections_lock);
  637. return -1;
  638. }
  639. mutex_unlock(&connections_lock);
  640. mutex_lock_nested(&con->sock_mutex, 0);
  641. if (!con->sock) {
  642. mutex_unlock(&con->sock_mutex);
  643. return -ENOTCONN;
  644. }
  645. result = kernel_accept(con->sock, &newsock, O_NONBLOCK);
  646. if (result < 0)
  647. goto accept_err;
  648. /* Get the connected socket's peer */
  649. memset(&peeraddr, 0, sizeof(peeraddr));
  650. len = newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, 2);
  651. if (len < 0) {
  652. result = -ECONNABORTED;
  653. goto accept_err;
  654. }
  655. /* Get the new node's NODEID */
  656. make_sockaddr(&peeraddr, 0, &len);
  657. if (addr_to_nodeid(&peeraddr, &nodeid)) {
  658. unsigned char *b=(unsigned char *)&peeraddr;
  659. log_print("connect from non cluster node");
  660. print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
  661. b, sizeof(struct sockaddr_storage));
  662. sock_release(newsock);
  663. mutex_unlock(&con->sock_mutex);
  664. return -1;
  665. }
  666. log_print("got connection from %d", nodeid);
  667. /* Check to see if we already have a connection to this node. This
  668. * could happen if the two nodes initiate a connection at roughly
  669. * the same time and the connections cross on the wire.
  670. * In this case we store the incoming one in "othercon"
  671. */
  672. newcon = nodeid2con(nodeid, GFP_NOFS);
  673. if (!newcon) {
  674. result = -ENOMEM;
  675. goto accept_err;
  676. }
  677. mutex_lock_nested(&newcon->sock_mutex, 1);
  678. if (newcon->sock) {
  679. struct connection *othercon = newcon->othercon;
  680. if (!othercon) {
  681. othercon = kmem_cache_zalloc(con_cache, GFP_NOFS);
  682. if (!othercon) {
  683. log_print("failed to allocate incoming socket");
  684. mutex_unlock(&newcon->sock_mutex);
  685. result = -ENOMEM;
  686. goto accept_err;
  687. }
  688. othercon->nodeid = nodeid;
  689. othercon->rx_action = receive_from_sock;
  690. mutex_init(&othercon->sock_mutex);
  691. INIT_LIST_HEAD(&othercon->writequeue);
  692. spin_lock_init(&othercon->writequeue_lock);
  693. INIT_WORK(&othercon->swork, process_send_sockets);
  694. INIT_WORK(&othercon->rwork, process_recv_sockets);
  695. set_bit(CF_IS_OTHERCON, &othercon->flags);
  696. }
  697. mutex_lock_nested(&othercon->sock_mutex, 2);
  698. if (!othercon->sock) {
  699. newcon->othercon = othercon;
  700. add_sock(newsock, othercon);
  701. addcon = othercon;
  702. mutex_unlock(&othercon->sock_mutex);
  703. }
  704. else {
  705. printk("Extra connection from node %d attempted\n", nodeid);
  706. result = -EAGAIN;
  707. mutex_unlock(&othercon->sock_mutex);
  708. mutex_unlock(&newcon->sock_mutex);
  709. goto accept_err;
  710. }
  711. }
  712. else {
  713. newcon->rx_action = receive_from_sock;
  714. /* accept copies the sk after we've saved the callbacks, so we
  715. don't want to save them a second time or comm errors will
  716. result in calling sk_error_report recursively. */
  717. add_sock(newsock, newcon);
  718. addcon = newcon;
  719. }
  720. mutex_unlock(&newcon->sock_mutex);
  721. /*
  722. * Add it to the active queue in case we got data
  723. * between processing the accept adding the socket
  724. * to the read_sockets list
  725. */
  726. if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
  727. queue_work(recv_workqueue, &addcon->rwork);
  728. mutex_unlock(&con->sock_mutex);
  729. return 0;
  730. accept_err:
  731. mutex_unlock(&con->sock_mutex);
  732. if (newsock)
  733. sock_release(newsock);
  734. if (result != -EAGAIN)
  735. log_print("error accepting connection from node: %d", result);
  736. return result;
  737. }
  738. static int sctp_accept_from_sock(struct connection *con)
  739. {
  740. /* Check that the new node is in the lockspace */
  741. struct sctp_prim prim;
  742. int nodeid;
  743. int prim_len, ret;
  744. int addr_len;
  745. struct connection *newcon;
  746. struct connection *addcon;
  747. struct socket *newsock;
  748. mutex_lock(&connections_lock);
  749. if (!dlm_allow_conn) {
  750. mutex_unlock(&connections_lock);
  751. return -1;
  752. }
  753. mutex_unlock(&connections_lock);
  754. mutex_lock_nested(&con->sock_mutex, 0);
  755. ret = kernel_accept(con->sock, &newsock, O_NONBLOCK);
  756. if (ret < 0)
  757. goto accept_err;
  758. memset(&prim, 0, sizeof(struct sctp_prim));
  759. prim_len = sizeof(struct sctp_prim);
  760. ret = kernel_getsockopt(newsock, IPPROTO_SCTP, SCTP_PRIMARY_ADDR,
  761. (char *)&prim, &prim_len);
  762. if (ret < 0) {
  763. log_print("getsockopt/sctp_primary_addr failed: %d", ret);
  764. goto accept_err;
  765. }
  766. make_sockaddr(&prim.ssp_addr, 0, &addr_len);
  767. ret = addr_to_nodeid(&prim.ssp_addr, &nodeid);
  768. if (ret) {
  769. unsigned char *b = (unsigned char *)&prim.ssp_addr;
  770. log_print("reject connect from unknown addr");
  771. print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
  772. b, sizeof(struct sockaddr_storage));
  773. goto accept_err;
  774. }
  775. newcon = nodeid2con(nodeid, GFP_NOFS);
  776. if (!newcon) {
  777. ret = -ENOMEM;
  778. goto accept_err;
  779. }
  780. mutex_lock_nested(&newcon->sock_mutex, 1);
  781. if (newcon->sock) {
  782. struct connection *othercon = newcon->othercon;
  783. if (!othercon) {
  784. othercon = kmem_cache_zalloc(con_cache, GFP_NOFS);
  785. if (!othercon) {
  786. log_print("failed to allocate incoming socket");
  787. mutex_unlock(&newcon->sock_mutex);
  788. ret = -ENOMEM;
  789. goto accept_err;
  790. }
  791. othercon->nodeid = nodeid;
  792. othercon->rx_action = receive_from_sock;
  793. mutex_init(&othercon->sock_mutex);
  794. INIT_LIST_HEAD(&othercon->writequeue);
  795. spin_lock_init(&othercon->writequeue_lock);
  796. INIT_WORK(&othercon->swork, process_send_sockets);
  797. INIT_WORK(&othercon->rwork, process_recv_sockets);
  798. set_bit(CF_IS_OTHERCON, &othercon->flags);
  799. }
  800. mutex_lock_nested(&othercon->sock_mutex, 2);
  801. if (!othercon->sock) {
  802. newcon->othercon = othercon;
  803. add_sock(newsock, othercon);
  804. addcon = othercon;
  805. mutex_unlock(&othercon->sock_mutex);
  806. } else {
  807. printk("Extra connection from node %d attempted\n", nodeid);
  808. ret = -EAGAIN;
  809. mutex_unlock(&othercon->sock_mutex);
  810. mutex_unlock(&newcon->sock_mutex);
  811. goto accept_err;
  812. }
  813. } else {
  814. newcon->rx_action = receive_from_sock;
  815. add_sock(newsock, newcon);
  816. addcon = newcon;
  817. }
  818. log_print("connected to %d", nodeid);
  819. mutex_unlock(&newcon->sock_mutex);
  820. /*
  821. * Add it to the active queue in case we got data
  822. * between processing the accept adding the socket
  823. * to the read_sockets list
  824. */
  825. if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
  826. queue_work(recv_workqueue, &addcon->rwork);
  827. mutex_unlock(&con->sock_mutex);
  828. return 0;
  829. accept_err:
  830. mutex_unlock(&con->sock_mutex);
  831. if (newsock)
  832. sock_release(newsock);
  833. if (ret != -EAGAIN)
  834. log_print("error accepting connection from node: %d", ret);
  835. return ret;
  836. }
  837. static void free_entry(struct writequeue_entry *e)
  838. {
  839. __free_page(e->page);
  840. kfree(e);
  841. }
  842. /*
  843. * writequeue_entry_complete - try to delete and free write queue entry
  844. * @e: write queue entry to try to delete
  845. * @completed: bytes completed
  846. *
  847. * writequeue_lock must be held.
  848. */
  849. static void writequeue_entry_complete(struct writequeue_entry *e, int completed)
  850. {
  851. e->offset += completed;
  852. e->len -= completed;
  853. if (e->len == 0 && e->users == 0) {
  854. list_del(&e->list);
  855. free_entry(e);
  856. }
  857. }
  858. /*
  859. * sctp_bind_addrs - bind a SCTP socket to all our addresses
  860. */
  861. static int sctp_bind_addrs(struct connection *con, uint16_t port)
  862. {
  863. struct sockaddr_storage localaddr;
  864. int i, addr_len, result = 0;
  865. for (i = 0; i < dlm_local_count; i++) {
  866. memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
  867. make_sockaddr(&localaddr, port, &addr_len);
  868. if (!i)
  869. result = kernel_bind(con->sock,
  870. (struct sockaddr *)&localaddr,
  871. addr_len);
  872. else
  873. result = kernel_setsockopt(con->sock, SOL_SCTP,
  874. SCTP_SOCKOPT_BINDX_ADD,
  875. (char *)&localaddr, addr_len);
  876. if (result < 0) {
  877. log_print("Can't bind to %d addr number %d, %d.\n",
  878. port, i + 1, result);
  879. break;
  880. }
  881. }
  882. return result;
  883. }
  884. /* Initiate an SCTP association.
  885. This is a special case of send_to_sock() in that we don't yet have a
  886. peeled-off socket for this association, so we use the listening socket
  887. and add the primary IP address of the remote node.
  888. */
  889. static void sctp_connect_to_sock(struct connection *con)
  890. {
  891. struct sockaddr_storage daddr;
  892. int one = 1;
  893. int result;
  894. int addr_len;
  895. struct socket *sock;
  896. struct timeval tv = { .tv_sec = 5, .tv_usec = 0 };
  897. if (con->nodeid == 0) {
  898. log_print("attempt to connect sock 0 foiled");
  899. return;
  900. }
  901. mutex_lock(&con->sock_mutex);
  902. /* Some odd races can cause double-connects, ignore them */
  903. if (con->retries++ > MAX_CONNECT_RETRIES)
  904. goto out;
  905. if (con->sock) {
  906. log_print("node %d already connected.", con->nodeid);
  907. goto out;
  908. }
  909. memset(&daddr, 0, sizeof(daddr));
  910. result = nodeid_to_addr(con->nodeid, &daddr, NULL, true);
  911. if (result < 0) {
  912. log_print("no address for nodeid %d", con->nodeid);
  913. goto out;
  914. }
  915. /* Create a socket to communicate with */
  916. result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
  917. SOCK_STREAM, IPPROTO_SCTP, &sock);
  918. if (result < 0)
  919. goto socket_err;
  920. con->rx_action = receive_from_sock;
  921. con->connect_action = sctp_connect_to_sock;
  922. add_sock(sock, con);
  923. /* Bind to all addresses. */
  924. if (sctp_bind_addrs(con, 0))
  925. goto bind_err;
  926. make_sockaddr(&daddr, dlm_config.ci_tcp_port, &addr_len);
  927. log_print("connecting to %d", con->nodeid);
  928. /* Turn off Nagle's algorithm */
  929. kernel_setsockopt(sock, SOL_SCTP, SCTP_NODELAY, (char *)&one,
  930. sizeof(one));
  931. /*
  932. * Make sock->ops->connect() function return in specified time,
  933. * since O_NONBLOCK argument in connect() function does not work here,
  934. * then, we should restore the default value of this attribute.
  935. */
  936. kernel_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,
  937. sizeof(tv));
  938. result = sock->ops->connect(sock, (struct sockaddr *)&daddr, addr_len,
  939. 0);
  940. memset(&tv, 0, sizeof(tv));
  941. kernel_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,
  942. sizeof(tv));
  943. if (result == -EINPROGRESS)
  944. result = 0;
  945. if (result == 0)
  946. goto out;
  947. bind_err:
  948. con->sock = NULL;
  949. sock_release(sock);
  950. socket_err:
  951. /*
  952. * Some errors are fatal and this list might need adjusting. For other
  953. * errors we try again until the max number of retries is reached.
  954. */
  955. if (result != -EHOSTUNREACH &&
  956. result != -ENETUNREACH &&
  957. result != -ENETDOWN &&
  958. result != -EINVAL &&
  959. result != -EPROTONOSUPPORT) {
  960. log_print("connect %d try %d error %d", con->nodeid,
  961. con->retries, result);
  962. mutex_unlock(&con->sock_mutex);
  963. msleep(1000);
  964. lowcomms_connect_sock(con);
  965. return;
  966. }
  967. out:
  968. mutex_unlock(&con->sock_mutex);
  969. }
  970. /* Connect a new socket to its peer */
  971. static void tcp_connect_to_sock(struct connection *con)
  972. {
  973. struct sockaddr_storage saddr, src_addr;
  974. int addr_len;
  975. struct socket *sock = NULL;
  976. int one = 1;
  977. int result;
  978. if (con->nodeid == 0) {
  979. log_print("attempt to connect sock 0 foiled");
  980. return;
  981. }
  982. mutex_lock(&con->sock_mutex);
  983. if (con->retries++ > MAX_CONNECT_RETRIES)
  984. goto out;
  985. /* Some odd races can cause double-connects, ignore them */
  986. if (con->sock)
  987. goto out;
  988. /* Create a socket to communicate with */
  989. result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
  990. SOCK_STREAM, IPPROTO_TCP, &sock);
  991. if (result < 0)
  992. goto out_err;
  993. memset(&saddr, 0, sizeof(saddr));
  994. result = nodeid_to_addr(con->nodeid, &saddr, NULL, false);
  995. if (result < 0) {
  996. log_print("no address for nodeid %d", con->nodeid);
  997. goto out_err;
  998. }
  999. con->rx_action = receive_from_sock;
  1000. con->connect_action = tcp_connect_to_sock;
  1001. add_sock(sock, con);
  1002. /* Bind to our cluster-known address connecting to avoid
  1003. routing problems */
  1004. memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
  1005. make_sockaddr(&src_addr, 0, &addr_len);
  1006. result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
  1007. addr_len);
  1008. if (result < 0) {
  1009. log_print("could not bind for connect: %d", result);
  1010. /* This *may* not indicate a critical error */
  1011. }
  1012. make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
  1013. log_print("connecting to %d", con->nodeid);
  1014. /* Turn off Nagle's algorithm */
  1015. kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
  1016. sizeof(one));
  1017. result = sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
  1018. O_NONBLOCK);
  1019. if (result == -EINPROGRESS)
  1020. result = 0;
  1021. if (result == 0)
  1022. goto out;
  1023. out_err:
  1024. if (con->sock) {
  1025. sock_release(con->sock);
  1026. con->sock = NULL;
  1027. } else if (sock) {
  1028. sock_release(sock);
  1029. }
  1030. /*
  1031. * Some errors are fatal and this list might need adjusting. For other
  1032. * errors we try again until the max number of retries is reached.
  1033. */
  1034. if (result != -EHOSTUNREACH &&
  1035. result != -ENETUNREACH &&
  1036. result != -ENETDOWN &&
  1037. result != -EINVAL &&
  1038. result != -EPROTONOSUPPORT) {
  1039. log_print("connect %d try %d error %d", con->nodeid,
  1040. con->retries, result);
  1041. mutex_unlock(&con->sock_mutex);
  1042. msleep(1000);
  1043. lowcomms_connect_sock(con);
  1044. return;
  1045. }
  1046. out:
  1047. mutex_unlock(&con->sock_mutex);
  1048. return;
  1049. }
  1050. static struct socket *tcp_create_listen_sock(struct connection *con,
  1051. struct sockaddr_storage *saddr)
  1052. {
  1053. struct socket *sock = NULL;
  1054. int result = 0;
  1055. int one = 1;
  1056. int addr_len;
  1057. if (dlm_local_addr[0]->ss_family == AF_INET)
  1058. addr_len = sizeof(struct sockaddr_in);
  1059. else
  1060. addr_len = sizeof(struct sockaddr_in6);
  1061. /* Create a socket to communicate with */
  1062. result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
  1063. SOCK_STREAM, IPPROTO_TCP, &sock);
  1064. if (result < 0) {
  1065. log_print("Can't create listening comms socket");
  1066. goto create_out;
  1067. }
  1068. /* Turn off Nagle's algorithm */
  1069. kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
  1070. sizeof(one));
  1071. result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  1072. (char *)&one, sizeof(one));
  1073. if (result < 0) {
  1074. log_print("Failed to set SO_REUSEADDR on socket: %d", result);
  1075. }
  1076. write_lock_bh(&sock->sk->sk_callback_lock);
  1077. sock->sk->sk_user_data = con;
  1078. save_listen_callbacks(sock);
  1079. con->rx_action = tcp_accept_from_sock;
  1080. con->connect_action = tcp_connect_to_sock;
  1081. write_unlock_bh(&sock->sk->sk_callback_lock);
  1082. /* Bind to our port */
  1083. make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
  1084. result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
  1085. if (result < 0) {
  1086. log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
  1087. sock_release(sock);
  1088. sock = NULL;
  1089. con->sock = NULL;
  1090. goto create_out;
  1091. }
  1092. result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
  1093. (char *)&one, sizeof(one));
  1094. if (result < 0) {
  1095. log_print("Set keepalive failed: %d", result);
  1096. }
  1097. result = sock->ops->listen(sock, 5);
  1098. if (result < 0) {
  1099. log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
  1100. sock_release(sock);
  1101. sock = NULL;
  1102. goto create_out;
  1103. }
  1104. create_out:
  1105. return sock;
  1106. }
  1107. /* Get local addresses */
  1108. static void init_local(void)
  1109. {
  1110. struct sockaddr_storage sas, *addr;
  1111. int i;
  1112. dlm_local_count = 0;
  1113. for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
  1114. if (dlm_our_addr(&sas, i))
  1115. break;
  1116. addr = kmemdup(&sas, sizeof(*addr), GFP_NOFS);
  1117. if (!addr)
  1118. break;
  1119. dlm_local_addr[dlm_local_count++] = addr;
  1120. }
  1121. }
  1122. /* Initialise SCTP socket and bind to all interfaces */
  1123. static int sctp_listen_for_all(void)
  1124. {
  1125. struct socket *sock = NULL;
  1126. int result = -EINVAL;
  1127. struct connection *con = nodeid2con(0, GFP_NOFS);
  1128. int bufsize = NEEDED_RMEM;
  1129. int one = 1;
  1130. if (!con)
  1131. return -ENOMEM;
  1132. log_print("Using SCTP for communications");
  1133. result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
  1134. SOCK_STREAM, IPPROTO_SCTP, &sock);
  1135. if (result < 0) {
  1136. log_print("Can't create comms socket, check SCTP is loaded");
  1137. goto out;
  1138. }
  1139. result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUFFORCE,
  1140. (char *)&bufsize, sizeof(bufsize));
  1141. if (result)
  1142. log_print("Error increasing buffer space on socket %d", result);
  1143. result = kernel_setsockopt(sock, SOL_SCTP, SCTP_NODELAY, (char *)&one,
  1144. sizeof(one));
  1145. if (result < 0)
  1146. log_print("Could not set SCTP NODELAY error %d\n", result);
  1147. write_lock_bh(&sock->sk->sk_callback_lock);
  1148. /* Init con struct */
  1149. sock->sk->sk_user_data = con;
  1150. save_listen_callbacks(sock);
  1151. con->sock = sock;
  1152. con->sock->sk->sk_data_ready = lowcomms_data_ready;
  1153. con->rx_action = sctp_accept_from_sock;
  1154. con->connect_action = sctp_connect_to_sock;
  1155. write_unlock_bh(&sock->sk->sk_callback_lock);
  1156. /* Bind to all addresses. */
  1157. if (sctp_bind_addrs(con, dlm_config.ci_tcp_port))
  1158. goto create_delsock;
  1159. result = sock->ops->listen(sock, 5);
  1160. if (result < 0) {
  1161. log_print("Can't set socket listening");
  1162. goto create_delsock;
  1163. }
  1164. return 0;
  1165. create_delsock:
  1166. sock_release(sock);
  1167. con->sock = NULL;
  1168. out:
  1169. return result;
  1170. }
  1171. static int tcp_listen_for_all(void)
  1172. {
  1173. struct socket *sock = NULL;
  1174. struct connection *con = nodeid2con(0, GFP_NOFS);
  1175. int result = -EINVAL;
  1176. if (!con)
  1177. return -ENOMEM;
  1178. /* We don't support multi-homed hosts */
  1179. if (dlm_local_addr[1] != NULL) {
  1180. log_print("TCP protocol can't handle multi-homed hosts, "
  1181. "try SCTP");
  1182. return -EINVAL;
  1183. }
  1184. log_print("Using TCP for communications");
  1185. sock = tcp_create_listen_sock(con, dlm_local_addr[0]);
  1186. if (sock) {
  1187. add_sock(sock, con);
  1188. result = 0;
  1189. }
  1190. else {
  1191. result = -EADDRINUSE;
  1192. }
  1193. return result;
  1194. }
  1195. static struct writequeue_entry *new_writequeue_entry(struct connection *con,
  1196. gfp_t allocation)
  1197. {
  1198. struct writequeue_entry *entry;
  1199. entry = kmalloc(sizeof(struct writequeue_entry), allocation);
  1200. if (!entry)
  1201. return NULL;
  1202. entry->page = alloc_page(allocation);
  1203. if (!entry->page) {
  1204. kfree(entry);
  1205. return NULL;
  1206. }
  1207. entry->offset = 0;
  1208. entry->len = 0;
  1209. entry->end = 0;
  1210. entry->users = 0;
  1211. entry->con = con;
  1212. return entry;
  1213. }
  1214. void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
  1215. {
  1216. struct connection *con;
  1217. struct writequeue_entry *e;
  1218. int offset = 0;
  1219. con = nodeid2con(nodeid, allocation);
  1220. if (!con)
  1221. return NULL;
  1222. spin_lock(&con->writequeue_lock);
  1223. e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
  1224. if ((&e->list == &con->writequeue) ||
  1225. (PAGE_SIZE - e->end < len)) {
  1226. e = NULL;
  1227. } else {
  1228. offset = e->end;
  1229. e->end += len;
  1230. e->users++;
  1231. }
  1232. spin_unlock(&con->writequeue_lock);
  1233. if (e) {
  1234. got_one:
  1235. *ppc = page_address(e->page) + offset;
  1236. return e;
  1237. }
  1238. e = new_writequeue_entry(con, allocation);
  1239. if (e) {
  1240. spin_lock(&con->writequeue_lock);
  1241. offset = e->end;
  1242. e->end += len;
  1243. e->users++;
  1244. list_add_tail(&e->list, &con->writequeue);
  1245. spin_unlock(&con->writequeue_lock);
  1246. goto got_one;
  1247. }
  1248. return NULL;
  1249. }
  1250. void dlm_lowcomms_commit_buffer(void *mh)
  1251. {
  1252. struct writequeue_entry *e = (struct writequeue_entry *)mh;
  1253. struct connection *con = e->con;
  1254. int users;
  1255. spin_lock(&con->writequeue_lock);
  1256. users = --e->users;
  1257. if (users)
  1258. goto out;
  1259. e->len = e->end - e->offset;
  1260. spin_unlock(&con->writequeue_lock);
  1261. queue_work(send_workqueue, &con->swork);
  1262. return;
  1263. out:
  1264. spin_unlock(&con->writequeue_lock);
  1265. return;
  1266. }
  1267. /* Send a message */
  1268. static void send_to_sock(struct connection *con)
  1269. {
  1270. int ret = 0;
  1271. const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
  1272. struct writequeue_entry *e;
  1273. int len, offset;
  1274. int count = 0;
  1275. mutex_lock(&con->sock_mutex);
  1276. if (con->sock == NULL)
  1277. goto out_connect;
  1278. spin_lock(&con->writequeue_lock);
  1279. for (;;) {
  1280. e = list_entry(con->writequeue.next, struct writequeue_entry,
  1281. list);
  1282. if ((struct list_head *) e == &con->writequeue)
  1283. break;
  1284. len = e->len;
  1285. offset = e->offset;
  1286. BUG_ON(len == 0 && e->users == 0);
  1287. spin_unlock(&con->writequeue_lock);
  1288. ret = 0;
  1289. if (len) {
  1290. ret = kernel_sendpage(con->sock, e->page, offset, len,
  1291. msg_flags);
  1292. if (ret == -EAGAIN || ret == 0) {
  1293. if (ret == -EAGAIN &&
  1294. test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) &&
  1295. !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
  1296. /* Notify TCP that we're limited by the
  1297. * application window size.
  1298. */
  1299. set_bit(SOCK_NOSPACE, &con->sock->flags);
  1300. con->sock->sk->sk_write_pending++;
  1301. }
  1302. cond_resched();
  1303. goto out;
  1304. } else if (ret < 0)
  1305. goto send_error;
  1306. }
  1307. /* Don't starve people filling buffers */
  1308. if (++count >= MAX_SEND_MSG_COUNT) {
  1309. cond_resched();
  1310. count = 0;
  1311. }
  1312. spin_lock(&con->writequeue_lock);
  1313. writequeue_entry_complete(e, ret);
  1314. }
  1315. spin_unlock(&con->writequeue_lock);
  1316. out:
  1317. mutex_unlock(&con->sock_mutex);
  1318. return;
  1319. send_error:
  1320. mutex_unlock(&con->sock_mutex);
  1321. close_connection(con, true, false, true);
  1322. /* Requeue the send work. When the work daemon runs again, it will try
  1323. a new connection, then call this function again. */
  1324. queue_work(send_workqueue, &con->swork);
  1325. return;
  1326. out_connect:
  1327. mutex_unlock(&con->sock_mutex);
  1328. queue_work(send_workqueue, &con->swork);
  1329. cond_resched();
  1330. }
  1331. static void clean_one_writequeue(struct connection *con)
  1332. {
  1333. struct writequeue_entry *e, *safe;
  1334. spin_lock(&con->writequeue_lock);
  1335. list_for_each_entry_safe(e, safe, &con->writequeue, list) {
  1336. list_del(&e->list);
  1337. free_entry(e);
  1338. }
  1339. spin_unlock(&con->writequeue_lock);
  1340. }
  1341. /* Called from recovery when it knows that a node has
  1342. left the cluster */
  1343. int dlm_lowcomms_close(int nodeid)
  1344. {
  1345. struct connection *con;
  1346. struct dlm_node_addr *na;
  1347. log_print("closing connection to node %d", nodeid);
  1348. con = nodeid2con(nodeid, 0);
  1349. if (con) {
  1350. set_bit(CF_CLOSE, &con->flags);
  1351. close_connection(con, true, true, true);
  1352. clean_one_writequeue(con);
  1353. }
  1354. spin_lock(&dlm_node_addrs_spin);
  1355. na = find_node_addr(nodeid);
  1356. if (na) {
  1357. list_del(&na->list);
  1358. while (na->addr_count--)
  1359. kfree(na->addr[na->addr_count]);
  1360. kfree(na);
  1361. }
  1362. spin_unlock(&dlm_node_addrs_spin);
  1363. return 0;
  1364. }
  1365. /* Receive workqueue function */
  1366. static void process_recv_sockets(struct work_struct *work)
  1367. {
  1368. struct connection *con = container_of(work, struct connection, rwork);
  1369. int err;
  1370. clear_bit(CF_READ_PENDING, &con->flags);
  1371. do {
  1372. err = con->rx_action(con);
  1373. } while (!err);
  1374. }
  1375. /* Send workqueue function */
  1376. static void process_send_sockets(struct work_struct *work)
  1377. {
  1378. struct connection *con = container_of(work, struct connection, swork);
  1379. clear_bit(CF_WRITE_PENDING, &con->flags);
  1380. if (con->sock == NULL) /* not mutex protected so check it inside too */
  1381. con->connect_action(con);
  1382. if (!list_empty(&con->writequeue))
  1383. send_to_sock(con);
  1384. }
  1385. /* Discard all entries on the write queues */
  1386. static void clean_writequeues(void)
  1387. {
  1388. foreach_conn(clean_one_writequeue);
  1389. }
  1390. static void work_stop(void)
  1391. {
  1392. if (recv_workqueue)
  1393. destroy_workqueue(recv_workqueue);
  1394. if (send_workqueue)
  1395. destroy_workqueue(send_workqueue);
  1396. }
  1397. static int work_start(void)
  1398. {
  1399. recv_workqueue = alloc_workqueue("dlm_recv",
  1400. WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
  1401. if (!recv_workqueue) {
  1402. log_print("can't start dlm_recv");
  1403. return -ENOMEM;
  1404. }
  1405. send_workqueue = alloc_workqueue("dlm_send",
  1406. WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
  1407. if (!send_workqueue) {
  1408. log_print("can't start dlm_send");
  1409. destroy_workqueue(recv_workqueue);
  1410. return -ENOMEM;
  1411. }
  1412. return 0;
  1413. }
  1414. static void _stop_conn(struct connection *con, bool and_other)
  1415. {
  1416. mutex_lock(&con->sock_mutex);
  1417. set_bit(CF_CLOSE, &con->flags);
  1418. set_bit(CF_READ_PENDING, &con->flags);
  1419. set_bit(CF_WRITE_PENDING, &con->flags);
  1420. if (con->sock && con->sock->sk) {
  1421. write_lock_bh(&con->sock->sk->sk_callback_lock);
  1422. con->sock->sk->sk_user_data = NULL;
  1423. write_unlock_bh(&con->sock->sk->sk_callback_lock);
  1424. }
  1425. if (con->othercon && and_other)
  1426. _stop_conn(con->othercon, false);
  1427. mutex_unlock(&con->sock_mutex);
  1428. }
  1429. static void stop_conn(struct connection *con)
  1430. {
  1431. _stop_conn(con, true);
  1432. }
  1433. static void free_conn(struct connection *con)
  1434. {
  1435. close_connection(con, true, true, true);
  1436. if (con->othercon)
  1437. kmem_cache_free(con_cache, con->othercon);
  1438. hlist_del(&con->list);
  1439. kmem_cache_free(con_cache, con);
  1440. }
  1441. static void work_flush(void)
  1442. {
  1443. int ok;
  1444. int i;
  1445. struct hlist_node *n;
  1446. struct connection *con;
  1447. if (recv_workqueue)
  1448. flush_workqueue(recv_workqueue);
  1449. if (send_workqueue)
  1450. flush_workqueue(send_workqueue);
  1451. do {
  1452. ok = 1;
  1453. foreach_conn(stop_conn);
  1454. if (recv_workqueue)
  1455. flush_workqueue(recv_workqueue);
  1456. if (send_workqueue)
  1457. flush_workqueue(send_workqueue);
  1458. for (i = 0; i < CONN_HASH_SIZE && ok; i++) {
  1459. hlist_for_each_entry_safe(con, n,
  1460. &connection_hash[i], list) {
  1461. ok &= test_bit(CF_READ_PENDING, &con->flags);
  1462. ok &= test_bit(CF_WRITE_PENDING, &con->flags);
  1463. if (con->othercon) {
  1464. ok &= test_bit(CF_READ_PENDING,
  1465. &con->othercon->flags);
  1466. ok &= test_bit(CF_WRITE_PENDING,
  1467. &con->othercon->flags);
  1468. }
  1469. }
  1470. }
  1471. } while (!ok);
  1472. }
  1473. void dlm_lowcomms_stop(void)
  1474. {
  1475. /* Set all the flags to prevent any
  1476. socket activity.
  1477. */
  1478. mutex_lock(&connections_lock);
  1479. dlm_allow_conn = 0;
  1480. mutex_unlock(&connections_lock);
  1481. work_flush();
  1482. clean_writequeues();
  1483. foreach_conn(free_conn);
  1484. work_stop();
  1485. kmem_cache_destroy(con_cache);
  1486. }
  1487. int dlm_lowcomms_start(void)
  1488. {
  1489. int error = -EINVAL;
  1490. struct connection *con;
  1491. int i;
  1492. for (i = 0; i < CONN_HASH_SIZE; i++)
  1493. INIT_HLIST_HEAD(&connection_hash[i]);
  1494. init_local();
  1495. if (!dlm_local_count) {
  1496. error = -ENOTCONN;
  1497. log_print("no local IP address has been set");
  1498. goto fail;
  1499. }
  1500. error = -ENOMEM;
  1501. con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
  1502. __alignof__(struct connection), 0,
  1503. NULL);
  1504. if (!con_cache)
  1505. goto fail;
  1506. error = work_start();
  1507. if (error)
  1508. goto fail_destroy;
  1509. dlm_allow_conn = 1;
  1510. /* Start listening */
  1511. if (dlm_config.ci_protocol == 0)
  1512. error = tcp_listen_for_all();
  1513. else
  1514. error = sctp_listen_for_all();
  1515. if (error)
  1516. goto fail_unlisten;
  1517. return 0;
  1518. fail_unlisten:
  1519. dlm_allow_conn = 0;
  1520. con = nodeid2con(0,0);
  1521. if (con) {
  1522. close_connection(con, false, true, true);
  1523. kmem_cache_free(con_cache, con);
  1524. }
  1525. fail_destroy:
  1526. kmem_cache_destroy(con_cache);
  1527. fail:
  1528. return error;
  1529. }
  1530. void dlm_lowcomms_exit(void)
  1531. {
  1532. struct dlm_node_addr *na, *safe;
  1533. spin_lock(&dlm_node_addrs_spin);
  1534. list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) {
  1535. list_del(&na->list);
  1536. while (na->addr_count--)
  1537. kfree(na->addr[na->addr_count]);
  1538. kfree(na);
  1539. }
  1540. spin_unlock(&dlm_node_addrs_spin);
  1541. }