lowcomms.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  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. int buflen;
  412. void (*orig_report)(struct sock *) = NULL;
  413. read_lock_bh(&sk->sk_callback_lock);
  414. con = sock2con(sk);
  415. if (con == NULL)
  416. goto out;
  417. orig_report = listen_sock.sk_error_report;
  418. if (con->sock == NULL ||
  419. kernel_getpeername(con->sock, (struct sockaddr *)&saddr, &buflen)) {
  420. printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
  421. "sending to node %d, port %d, "
  422. "sk_err=%d/%d\n", dlm_our_nodeid(),
  423. con->nodeid, dlm_config.ci_tcp_port,
  424. sk->sk_err, sk->sk_err_soft);
  425. } else if (saddr.ss_family == AF_INET) {
  426. struct sockaddr_in *sin4 = (struct sockaddr_in *)&saddr;
  427. printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
  428. "sending to node %d at %pI4, port %d, "
  429. "sk_err=%d/%d\n", dlm_our_nodeid(),
  430. con->nodeid, &sin4->sin_addr.s_addr,
  431. dlm_config.ci_tcp_port, sk->sk_err,
  432. sk->sk_err_soft);
  433. } else {
  434. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&saddr;
  435. printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
  436. "sending to node %d at %u.%u.%u.%u, "
  437. "port %d, sk_err=%d/%d\n", dlm_our_nodeid(),
  438. con->nodeid, sin6->sin6_addr.s6_addr32[0],
  439. sin6->sin6_addr.s6_addr32[1],
  440. sin6->sin6_addr.s6_addr32[2],
  441. sin6->sin6_addr.s6_addr32[3],
  442. dlm_config.ci_tcp_port, sk->sk_err,
  443. sk->sk_err_soft);
  444. }
  445. out:
  446. read_unlock_bh(&sk->sk_callback_lock);
  447. if (orig_report)
  448. orig_report(sk);
  449. }
  450. /* Note: sk_callback_lock must be locked before calling this function. */
  451. static void save_listen_callbacks(struct socket *sock)
  452. {
  453. struct sock *sk = sock->sk;
  454. listen_sock.sk_data_ready = sk->sk_data_ready;
  455. listen_sock.sk_state_change = sk->sk_state_change;
  456. listen_sock.sk_write_space = sk->sk_write_space;
  457. listen_sock.sk_error_report = sk->sk_error_report;
  458. }
  459. static void restore_callbacks(struct socket *sock)
  460. {
  461. struct sock *sk = sock->sk;
  462. write_lock_bh(&sk->sk_callback_lock);
  463. sk->sk_user_data = NULL;
  464. sk->sk_data_ready = listen_sock.sk_data_ready;
  465. sk->sk_state_change = listen_sock.sk_state_change;
  466. sk->sk_write_space = listen_sock.sk_write_space;
  467. sk->sk_error_report = listen_sock.sk_error_report;
  468. write_unlock_bh(&sk->sk_callback_lock);
  469. }
  470. /* Make a socket active */
  471. static void add_sock(struct socket *sock, struct connection *con)
  472. {
  473. struct sock *sk = sock->sk;
  474. write_lock_bh(&sk->sk_callback_lock);
  475. con->sock = sock;
  476. sk->sk_user_data = con;
  477. /* Install a data_ready callback */
  478. sk->sk_data_ready = lowcomms_data_ready;
  479. sk->sk_write_space = lowcomms_write_space;
  480. sk->sk_state_change = lowcomms_state_change;
  481. sk->sk_allocation = GFP_NOFS;
  482. sk->sk_error_report = lowcomms_error_report;
  483. write_unlock_bh(&sk->sk_callback_lock);
  484. }
  485. /* Add the port number to an IPv6 or 4 sockaddr and return the address
  486. length */
  487. static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
  488. int *addr_len)
  489. {
  490. saddr->ss_family = dlm_local_addr[0]->ss_family;
  491. if (saddr->ss_family == AF_INET) {
  492. struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
  493. in4_addr->sin_port = cpu_to_be16(port);
  494. *addr_len = sizeof(struct sockaddr_in);
  495. memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
  496. } else {
  497. struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
  498. in6_addr->sin6_port = cpu_to_be16(port);
  499. *addr_len = sizeof(struct sockaddr_in6);
  500. }
  501. memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
  502. }
  503. /* Close a remote connection and tidy up */
  504. static void close_connection(struct connection *con, bool and_other,
  505. bool tx, bool rx)
  506. {
  507. bool closing = test_and_set_bit(CF_CLOSING, &con->flags);
  508. if (tx && !closing && cancel_work_sync(&con->swork)) {
  509. log_print("canceled swork for node %d", con->nodeid);
  510. clear_bit(CF_WRITE_PENDING, &con->flags);
  511. }
  512. if (rx && !closing && cancel_work_sync(&con->rwork)) {
  513. log_print("canceled rwork for node %d", con->nodeid);
  514. clear_bit(CF_READ_PENDING, &con->flags);
  515. }
  516. mutex_lock(&con->sock_mutex);
  517. if (con->sock) {
  518. restore_callbacks(con->sock);
  519. sock_release(con->sock);
  520. con->sock = NULL;
  521. }
  522. if (con->othercon && and_other) {
  523. /* Will only re-enter once. */
  524. close_connection(con->othercon, false, true, true);
  525. }
  526. if (con->rx_page) {
  527. __free_page(con->rx_page);
  528. con->rx_page = NULL;
  529. }
  530. con->retries = 0;
  531. mutex_unlock(&con->sock_mutex);
  532. clear_bit(CF_CLOSING, &con->flags);
  533. }
  534. /* Data received from remote end */
  535. static int receive_from_sock(struct connection *con)
  536. {
  537. int ret = 0;
  538. struct msghdr msg = {};
  539. struct kvec iov[2];
  540. unsigned len;
  541. int r;
  542. int call_again_soon = 0;
  543. int nvec;
  544. mutex_lock(&con->sock_mutex);
  545. if (con->sock == NULL) {
  546. ret = -EAGAIN;
  547. goto out_close;
  548. }
  549. if (con->nodeid == 0) {
  550. ret = -EINVAL;
  551. goto out_close;
  552. }
  553. if (con->rx_page == NULL) {
  554. /*
  555. * This doesn't need to be atomic, but I think it should
  556. * improve performance if it is.
  557. */
  558. con->rx_page = alloc_page(GFP_ATOMIC);
  559. if (con->rx_page == NULL)
  560. goto out_resched;
  561. cbuf_init(&con->cb, PAGE_SIZE);
  562. }
  563. /*
  564. * iov[0] is the bit of the circular buffer between the current end
  565. * point (cb.base + cb.len) and the end of the buffer.
  566. */
  567. iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
  568. iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
  569. iov[1].iov_len = 0;
  570. nvec = 1;
  571. /*
  572. * iov[1] is the bit of the circular buffer between the start of the
  573. * buffer and the start of the currently used section (cb.base)
  574. */
  575. if (cbuf_data(&con->cb) >= con->cb.base) {
  576. iov[0].iov_len = PAGE_SIZE - cbuf_data(&con->cb);
  577. iov[1].iov_len = con->cb.base;
  578. iov[1].iov_base = page_address(con->rx_page);
  579. nvec = 2;
  580. }
  581. len = iov[0].iov_len + iov[1].iov_len;
  582. iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, iov, nvec, len);
  583. r = ret = sock_recvmsg(con->sock, &msg, MSG_DONTWAIT | MSG_NOSIGNAL);
  584. if (ret <= 0)
  585. goto out_close;
  586. else if (ret == len)
  587. call_again_soon = 1;
  588. cbuf_add(&con->cb, ret);
  589. ret = dlm_process_incoming_buffer(con->nodeid,
  590. page_address(con->rx_page),
  591. con->cb.base, con->cb.len,
  592. PAGE_SIZE);
  593. if (ret == -EBADMSG) {
  594. log_print("lowcomms: addr=%p, base=%u, len=%u, read=%d",
  595. page_address(con->rx_page), con->cb.base,
  596. con->cb.len, r);
  597. }
  598. if (ret < 0)
  599. goto out_close;
  600. cbuf_eat(&con->cb, ret);
  601. if (cbuf_empty(&con->cb) && !call_again_soon) {
  602. __free_page(con->rx_page);
  603. con->rx_page = NULL;
  604. }
  605. if (call_again_soon)
  606. goto out_resched;
  607. mutex_unlock(&con->sock_mutex);
  608. return 0;
  609. out_resched:
  610. if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
  611. queue_work(recv_workqueue, &con->rwork);
  612. mutex_unlock(&con->sock_mutex);
  613. return -EAGAIN;
  614. out_close:
  615. mutex_unlock(&con->sock_mutex);
  616. if (ret != -EAGAIN) {
  617. close_connection(con, true, true, false);
  618. /* Reconnect when there is something to send */
  619. }
  620. /* Don't return success if we really got EOF */
  621. if (ret == 0)
  622. ret = -EAGAIN;
  623. return ret;
  624. }
  625. /* Listening socket is busy, accept a connection */
  626. static int tcp_accept_from_sock(struct connection *con)
  627. {
  628. int result;
  629. struct sockaddr_storage peeraddr;
  630. struct socket *newsock;
  631. int len;
  632. int nodeid;
  633. struct connection *newcon;
  634. struct connection *addcon;
  635. mutex_lock(&connections_lock);
  636. if (!dlm_allow_conn) {
  637. mutex_unlock(&connections_lock);
  638. return -1;
  639. }
  640. mutex_unlock(&connections_lock);
  641. mutex_lock_nested(&con->sock_mutex, 0);
  642. if (!con->sock) {
  643. mutex_unlock(&con->sock_mutex);
  644. return -ENOTCONN;
  645. }
  646. result = kernel_accept(con->sock, &newsock, O_NONBLOCK);
  647. if (result < 0)
  648. goto accept_err;
  649. /* Get the connected socket's peer */
  650. memset(&peeraddr, 0, sizeof(peeraddr));
  651. if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr,
  652. &len, 2)) {
  653. result = -ECONNABORTED;
  654. goto accept_err;
  655. }
  656. /* Get the new node's NODEID */
  657. make_sockaddr(&peeraddr, 0, &len);
  658. if (addr_to_nodeid(&peeraddr, &nodeid)) {
  659. unsigned char *b=(unsigned char *)&peeraddr;
  660. log_print("connect from non cluster node");
  661. print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
  662. b, sizeof(struct sockaddr_storage));
  663. sock_release(newsock);
  664. mutex_unlock(&con->sock_mutex);
  665. return -1;
  666. }
  667. log_print("got connection from %d", nodeid);
  668. /* Check to see if we already have a connection to this node. This
  669. * could happen if the two nodes initiate a connection at roughly
  670. * the same time and the connections cross on the wire.
  671. * In this case we store the incoming one in "othercon"
  672. */
  673. newcon = nodeid2con(nodeid, GFP_NOFS);
  674. if (!newcon) {
  675. result = -ENOMEM;
  676. goto accept_err;
  677. }
  678. mutex_lock_nested(&newcon->sock_mutex, 1);
  679. if (newcon->sock) {
  680. struct connection *othercon = newcon->othercon;
  681. if (!othercon) {
  682. othercon = kmem_cache_zalloc(con_cache, GFP_NOFS);
  683. if (!othercon) {
  684. log_print("failed to allocate incoming socket");
  685. mutex_unlock(&newcon->sock_mutex);
  686. result = -ENOMEM;
  687. goto accept_err;
  688. }
  689. othercon->nodeid = nodeid;
  690. othercon->rx_action = receive_from_sock;
  691. mutex_init(&othercon->sock_mutex);
  692. INIT_LIST_HEAD(&othercon->writequeue);
  693. spin_lock_init(&othercon->writequeue_lock);
  694. INIT_WORK(&othercon->swork, process_send_sockets);
  695. INIT_WORK(&othercon->rwork, process_recv_sockets);
  696. set_bit(CF_IS_OTHERCON, &othercon->flags);
  697. }
  698. mutex_lock_nested(&othercon->sock_mutex, 2);
  699. if (!othercon->sock) {
  700. newcon->othercon = othercon;
  701. add_sock(newsock, othercon);
  702. addcon = othercon;
  703. mutex_unlock(&othercon->sock_mutex);
  704. }
  705. else {
  706. printk("Extra connection from node %d attempted\n", nodeid);
  707. result = -EAGAIN;
  708. mutex_unlock(&othercon->sock_mutex);
  709. mutex_unlock(&newcon->sock_mutex);
  710. goto accept_err;
  711. }
  712. }
  713. else {
  714. newcon->rx_action = receive_from_sock;
  715. /* accept copies the sk after we've saved the callbacks, so we
  716. don't want to save them a second time or comm errors will
  717. result in calling sk_error_report recursively. */
  718. add_sock(newsock, newcon);
  719. addcon = newcon;
  720. }
  721. mutex_unlock(&newcon->sock_mutex);
  722. /*
  723. * Add it to the active queue in case we got data
  724. * between processing the accept adding the socket
  725. * to the read_sockets list
  726. */
  727. if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
  728. queue_work(recv_workqueue, &addcon->rwork);
  729. mutex_unlock(&con->sock_mutex);
  730. return 0;
  731. accept_err:
  732. mutex_unlock(&con->sock_mutex);
  733. if (newsock)
  734. sock_release(newsock);
  735. if (result != -EAGAIN)
  736. log_print("error accepting connection from node: %d", result);
  737. return result;
  738. }
  739. static int sctp_accept_from_sock(struct connection *con)
  740. {
  741. /* Check that the new node is in the lockspace */
  742. struct sctp_prim prim;
  743. int nodeid;
  744. int prim_len, ret;
  745. int addr_len;
  746. struct connection *newcon;
  747. struct connection *addcon;
  748. struct socket *newsock;
  749. mutex_lock(&connections_lock);
  750. if (!dlm_allow_conn) {
  751. mutex_unlock(&connections_lock);
  752. return -1;
  753. }
  754. mutex_unlock(&connections_lock);
  755. mutex_lock_nested(&con->sock_mutex, 0);
  756. ret = kernel_accept(con->sock, &newsock, O_NONBLOCK);
  757. if (ret < 0)
  758. goto accept_err;
  759. memset(&prim, 0, sizeof(struct sctp_prim));
  760. prim_len = sizeof(struct sctp_prim);
  761. ret = kernel_getsockopt(newsock, IPPROTO_SCTP, SCTP_PRIMARY_ADDR,
  762. (char *)&prim, &prim_len);
  763. if (ret < 0) {
  764. log_print("getsockopt/sctp_primary_addr failed: %d", ret);
  765. goto accept_err;
  766. }
  767. make_sockaddr(&prim.ssp_addr, 0, &addr_len);
  768. ret = addr_to_nodeid(&prim.ssp_addr, &nodeid);
  769. if (ret) {
  770. unsigned char *b = (unsigned char *)&prim.ssp_addr;
  771. log_print("reject connect from unknown addr");
  772. print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
  773. b, sizeof(struct sockaddr_storage));
  774. goto accept_err;
  775. }
  776. newcon = nodeid2con(nodeid, GFP_NOFS);
  777. if (!newcon) {
  778. ret = -ENOMEM;
  779. goto accept_err;
  780. }
  781. mutex_lock_nested(&newcon->sock_mutex, 1);
  782. if (newcon->sock) {
  783. struct connection *othercon = newcon->othercon;
  784. if (!othercon) {
  785. othercon = kmem_cache_zalloc(con_cache, GFP_NOFS);
  786. if (!othercon) {
  787. log_print("failed to allocate incoming socket");
  788. mutex_unlock(&newcon->sock_mutex);
  789. ret = -ENOMEM;
  790. goto accept_err;
  791. }
  792. othercon->nodeid = nodeid;
  793. othercon->rx_action = receive_from_sock;
  794. mutex_init(&othercon->sock_mutex);
  795. INIT_LIST_HEAD(&othercon->writequeue);
  796. spin_lock_init(&othercon->writequeue_lock);
  797. INIT_WORK(&othercon->swork, process_send_sockets);
  798. INIT_WORK(&othercon->rwork, process_recv_sockets);
  799. set_bit(CF_IS_OTHERCON, &othercon->flags);
  800. }
  801. mutex_lock_nested(&othercon->sock_mutex, 2);
  802. if (!othercon->sock) {
  803. newcon->othercon = othercon;
  804. add_sock(newsock, othercon);
  805. addcon = othercon;
  806. mutex_unlock(&othercon->sock_mutex);
  807. } else {
  808. printk("Extra connection from node %d attempted\n", nodeid);
  809. ret = -EAGAIN;
  810. mutex_unlock(&othercon->sock_mutex);
  811. mutex_unlock(&newcon->sock_mutex);
  812. goto accept_err;
  813. }
  814. } else {
  815. newcon->rx_action = receive_from_sock;
  816. add_sock(newsock, newcon);
  817. addcon = newcon;
  818. }
  819. log_print("connected to %d", nodeid);
  820. mutex_unlock(&newcon->sock_mutex);
  821. /*
  822. * Add it to the active queue in case we got data
  823. * between processing the accept adding the socket
  824. * to the read_sockets list
  825. */
  826. if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
  827. queue_work(recv_workqueue, &addcon->rwork);
  828. mutex_unlock(&con->sock_mutex);
  829. return 0;
  830. accept_err:
  831. mutex_unlock(&con->sock_mutex);
  832. if (newsock)
  833. sock_release(newsock);
  834. if (ret != -EAGAIN)
  835. log_print("error accepting connection from node: %d", ret);
  836. return ret;
  837. }
  838. static void free_entry(struct writequeue_entry *e)
  839. {
  840. __free_page(e->page);
  841. kfree(e);
  842. }
  843. /*
  844. * writequeue_entry_complete - try to delete and free write queue entry
  845. * @e: write queue entry to try to delete
  846. * @completed: bytes completed
  847. *
  848. * writequeue_lock must be held.
  849. */
  850. static void writequeue_entry_complete(struct writequeue_entry *e, int completed)
  851. {
  852. e->offset += completed;
  853. e->len -= completed;
  854. if (e->len == 0 && e->users == 0) {
  855. list_del(&e->list);
  856. free_entry(e);
  857. }
  858. }
  859. /*
  860. * sctp_bind_addrs - bind a SCTP socket to all our addresses
  861. */
  862. static int sctp_bind_addrs(struct connection *con, uint16_t port)
  863. {
  864. struct sockaddr_storage localaddr;
  865. int i, addr_len, result = 0;
  866. for (i = 0; i < dlm_local_count; i++) {
  867. memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
  868. make_sockaddr(&localaddr, port, &addr_len);
  869. if (!i)
  870. result = kernel_bind(con->sock,
  871. (struct sockaddr *)&localaddr,
  872. addr_len);
  873. else
  874. result = kernel_setsockopt(con->sock, SOL_SCTP,
  875. SCTP_SOCKOPT_BINDX_ADD,
  876. (char *)&localaddr, addr_len);
  877. if (result < 0) {
  878. log_print("Can't bind to %d addr number %d, %d.\n",
  879. port, i + 1, result);
  880. break;
  881. }
  882. }
  883. return result;
  884. }
  885. /* Initiate an SCTP association.
  886. This is a special case of send_to_sock() in that we don't yet have a
  887. peeled-off socket for this association, so we use the listening socket
  888. and add the primary IP address of the remote node.
  889. */
  890. static void sctp_connect_to_sock(struct connection *con)
  891. {
  892. struct sockaddr_storage daddr;
  893. int one = 1;
  894. int result;
  895. int addr_len;
  896. struct socket *sock;
  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_TCP, TCP_NODELAY, (char *)&one,
  930. sizeof(one));
  931. result = sock->ops->connect(sock, (struct sockaddr *)&daddr, addr_len,
  932. O_NONBLOCK);
  933. if (result == -EINPROGRESS)
  934. result = 0;
  935. if (result == 0)
  936. goto out;
  937. bind_err:
  938. con->sock = NULL;
  939. sock_release(sock);
  940. socket_err:
  941. /*
  942. * Some errors are fatal and this list might need adjusting. For other
  943. * errors we try again until the max number of retries is reached.
  944. */
  945. if (result != -EHOSTUNREACH &&
  946. result != -ENETUNREACH &&
  947. result != -ENETDOWN &&
  948. result != -EINVAL &&
  949. result != -EPROTONOSUPPORT) {
  950. log_print("connect %d try %d error %d", con->nodeid,
  951. con->retries, result);
  952. mutex_unlock(&con->sock_mutex);
  953. msleep(1000);
  954. lowcomms_connect_sock(con);
  955. return;
  956. }
  957. out:
  958. mutex_unlock(&con->sock_mutex);
  959. }
  960. /* Connect a new socket to its peer */
  961. static void tcp_connect_to_sock(struct connection *con)
  962. {
  963. struct sockaddr_storage saddr, src_addr;
  964. int addr_len;
  965. struct socket *sock = NULL;
  966. int one = 1;
  967. int result;
  968. if (con->nodeid == 0) {
  969. log_print("attempt to connect sock 0 foiled");
  970. return;
  971. }
  972. mutex_lock(&con->sock_mutex);
  973. if (con->retries++ > MAX_CONNECT_RETRIES)
  974. goto out;
  975. /* Some odd races can cause double-connects, ignore them */
  976. if (con->sock)
  977. goto out;
  978. /* Create a socket to communicate with */
  979. result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
  980. SOCK_STREAM, IPPROTO_TCP, &sock);
  981. if (result < 0)
  982. goto out_err;
  983. memset(&saddr, 0, sizeof(saddr));
  984. result = nodeid_to_addr(con->nodeid, &saddr, NULL, false);
  985. if (result < 0) {
  986. log_print("no address for nodeid %d", con->nodeid);
  987. goto out_err;
  988. }
  989. con->rx_action = receive_from_sock;
  990. con->connect_action = tcp_connect_to_sock;
  991. add_sock(sock, con);
  992. /* Bind to our cluster-known address connecting to avoid
  993. routing problems */
  994. memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
  995. make_sockaddr(&src_addr, 0, &addr_len);
  996. result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
  997. addr_len);
  998. if (result < 0) {
  999. log_print("could not bind for connect: %d", result);
  1000. /* This *may* not indicate a critical error */
  1001. }
  1002. make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
  1003. log_print("connecting to %d", con->nodeid);
  1004. /* Turn off Nagle's algorithm */
  1005. kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
  1006. sizeof(one));
  1007. result = sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
  1008. O_NONBLOCK);
  1009. if (result == -EINPROGRESS)
  1010. result = 0;
  1011. if (result == 0)
  1012. goto out;
  1013. out_err:
  1014. if (con->sock) {
  1015. sock_release(con->sock);
  1016. con->sock = NULL;
  1017. } else if (sock) {
  1018. sock_release(sock);
  1019. }
  1020. /*
  1021. * Some errors are fatal and this list might need adjusting. For other
  1022. * errors we try again until the max number of retries is reached.
  1023. */
  1024. if (result != -EHOSTUNREACH &&
  1025. result != -ENETUNREACH &&
  1026. result != -ENETDOWN &&
  1027. result != -EINVAL &&
  1028. result != -EPROTONOSUPPORT) {
  1029. log_print("connect %d try %d error %d", con->nodeid,
  1030. con->retries, result);
  1031. mutex_unlock(&con->sock_mutex);
  1032. msleep(1000);
  1033. lowcomms_connect_sock(con);
  1034. return;
  1035. }
  1036. out:
  1037. mutex_unlock(&con->sock_mutex);
  1038. return;
  1039. }
  1040. static struct socket *tcp_create_listen_sock(struct connection *con,
  1041. struct sockaddr_storage *saddr)
  1042. {
  1043. struct socket *sock = NULL;
  1044. int result = 0;
  1045. int one = 1;
  1046. int addr_len;
  1047. if (dlm_local_addr[0]->ss_family == AF_INET)
  1048. addr_len = sizeof(struct sockaddr_in);
  1049. else
  1050. addr_len = sizeof(struct sockaddr_in6);
  1051. /* Create a socket to communicate with */
  1052. result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
  1053. SOCK_STREAM, IPPROTO_TCP, &sock);
  1054. if (result < 0) {
  1055. log_print("Can't create listening comms socket");
  1056. goto create_out;
  1057. }
  1058. /* Turn off Nagle's algorithm */
  1059. kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
  1060. sizeof(one));
  1061. result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  1062. (char *)&one, sizeof(one));
  1063. if (result < 0) {
  1064. log_print("Failed to set SO_REUSEADDR on socket: %d", result);
  1065. }
  1066. write_lock_bh(&sock->sk->sk_callback_lock);
  1067. sock->sk->sk_user_data = con;
  1068. save_listen_callbacks(sock);
  1069. con->rx_action = tcp_accept_from_sock;
  1070. con->connect_action = tcp_connect_to_sock;
  1071. write_unlock_bh(&sock->sk->sk_callback_lock);
  1072. /* Bind to our port */
  1073. make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
  1074. result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
  1075. if (result < 0) {
  1076. log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
  1077. sock_release(sock);
  1078. sock = NULL;
  1079. con->sock = NULL;
  1080. goto create_out;
  1081. }
  1082. result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
  1083. (char *)&one, sizeof(one));
  1084. if (result < 0) {
  1085. log_print("Set keepalive failed: %d", result);
  1086. }
  1087. result = sock->ops->listen(sock, 5);
  1088. if (result < 0) {
  1089. log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
  1090. sock_release(sock);
  1091. sock = NULL;
  1092. goto create_out;
  1093. }
  1094. create_out:
  1095. return sock;
  1096. }
  1097. /* Get local addresses */
  1098. static void init_local(void)
  1099. {
  1100. struct sockaddr_storage sas, *addr;
  1101. int i;
  1102. dlm_local_count = 0;
  1103. for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
  1104. if (dlm_our_addr(&sas, i))
  1105. break;
  1106. addr = kmemdup(&sas, sizeof(*addr), GFP_NOFS);
  1107. if (!addr)
  1108. break;
  1109. dlm_local_addr[dlm_local_count++] = addr;
  1110. }
  1111. }
  1112. /* Initialise SCTP socket and bind to all interfaces */
  1113. static int sctp_listen_for_all(void)
  1114. {
  1115. struct socket *sock = NULL;
  1116. int result = -EINVAL;
  1117. struct connection *con = nodeid2con(0, GFP_NOFS);
  1118. int bufsize = NEEDED_RMEM;
  1119. int one = 1;
  1120. if (!con)
  1121. return -ENOMEM;
  1122. log_print("Using SCTP for communications");
  1123. result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
  1124. SOCK_STREAM, IPPROTO_SCTP, &sock);
  1125. if (result < 0) {
  1126. log_print("Can't create comms socket, check SCTP is loaded");
  1127. goto out;
  1128. }
  1129. result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUFFORCE,
  1130. (char *)&bufsize, sizeof(bufsize));
  1131. if (result)
  1132. log_print("Error increasing buffer space on socket %d", result);
  1133. result = kernel_setsockopt(sock, SOL_SCTP, SCTP_NODELAY, (char *)&one,
  1134. sizeof(one));
  1135. if (result < 0)
  1136. log_print("Could not set SCTP NODELAY error %d\n", result);
  1137. write_lock_bh(&sock->sk->sk_callback_lock);
  1138. /* Init con struct */
  1139. sock->sk->sk_user_data = con;
  1140. save_listen_callbacks(sock);
  1141. con->sock = sock;
  1142. con->sock->sk->sk_data_ready = lowcomms_data_ready;
  1143. con->rx_action = sctp_accept_from_sock;
  1144. con->connect_action = sctp_connect_to_sock;
  1145. write_unlock_bh(&sock->sk->sk_callback_lock);
  1146. /* Bind to all addresses. */
  1147. if (sctp_bind_addrs(con, dlm_config.ci_tcp_port))
  1148. goto create_delsock;
  1149. result = sock->ops->listen(sock, 5);
  1150. if (result < 0) {
  1151. log_print("Can't set socket listening");
  1152. goto create_delsock;
  1153. }
  1154. return 0;
  1155. create_delsock:
  1156. sock_release(sock);
  1157. con->sock = NULL;
  1158. out:
  1159. return result;
  1160. }
  1161. static int tcp_listen_for_all(void)
  1162. {
  1163. struct socket *sock = NULL;
  1164. struct connection *con = nodeid2con(0, GFP_NOFS);
  1165. int result = -EINVAL;
  1166. if (!con)
  1167. return -ENOMEM;
  1168. /* We don't support multi-homed hosts */
  1169. if (dlm_local_addr[1] != NULL) {
  1170. log_print("TCP protocol can't handle multi-homed hosts, "
  1171. "try SCTP");
  1172. return -EINVAL;
  1173. }
  1174. log_print("Using TCP for communications");
  1175. sock = tcp_create_listen_sock(con, dlm_local_addr[0]);
  1176. if (sock) {
  1177. add_sock(sock, con);
  1178. result = 0;
  1179. }
  1180. else {
  1181. result = -EADDRINUSE;
  1182. }
  1183. return result;
  1184. }
  1185. static struct writequeue_entry *new_writequeue_entry(struct connection *con,
  1186. gfp_t allocation)
  1187. {
  1188. struct writequeue_entry *entry;
  1189. entry = kmalloc(sizeof(struct writequeue_entry), allocation);
  1190. if (!entry)
  1191. return NULL;
  1192. entry->page = alloc_page(allocation);
  1193. if (!entry->page) {
  1194. kfree(entry);
  1195. return NULL;
  1196. }
  1197. entry->offset = 0;
  1198. entry->len = 0;
  1199. entry->end = 0;
  1200. entry->users = 0;
  1201. entry->con = con;
  1202. return entry;
  1203. }
  1204. void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
  1205. {
  1206. struct connection *con;
  1207. struct writequeue_entry *e;
  1208. int offset = 0;
  1209. con = nodeid2con(nodeid, allocation);
  1210. if (!con)
  1211. return NULL;
  1212. spin_lock(&con->writequeue_lock);
  1213. e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
  1214. if ((&e->list == &con->writequeue) ||
  1215. (PAGE_SIZE - e->end < len)) {
  1216. e = NULL;
  1217. } else {
  1218. offset = e->end;
  1219. e->end += len;
  1220. e->users++;
  1221. }
  1222. spin_unlock(&con->writequeue_lock);
  1223. if (e) {
  1224. got_one:
  1225. *ppc = page_address(e->page) + offset;
  1226. return e;
  1227. }
  1228. e = new_writequeue_entry(con, allocation);
  1229. if (e) {
  1230. spin_lock(&con->writequeue_lock);
  1231. offset = e->end;
  1232. e->end += len;
  1233. e->users++;
  1234. list_add_tail(&e->list, &con->writequeue);
  1235. spin_unlock(&con->writequeue_lock);
  1236. goto got_one;
  1237. }
  1238. return NULL;
  1239. }
  1240. void dlm_lowcomms_commit_buffer(void *mh)
  1241. {
  1242. struct writequeue_entry *e = (struct writequeue_entry *)mh;
  1243. struct connection *con = e->con;
  1244. int users;
  1245. spin_lock(&con->writequeue_lock);
  1246. users = --e->users;
  1247. if (users)
  1248. goto out;
  1249. e->len = e->end - e->offset;
  1250. spin_unlock(&con->writequeue_lock);
  1251. queue_work(send_workqueue, &con->swork);
  1252. return;
  1253. out:
  1254. spin_unlock(&con->writequeue_lock);
  1255. return;
  1256. }
  1257. /* Send a message */
  1258. static void send_to_sock(struct connection *con)
  1259. {
  1260. int ret = 0;
  1261. const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
  1262. struct writequeue_entry *e;
  1263. int len, offset;
  1264. int count = 0;
  1265. mutex_lock(&con->sock_mutex);
  1266. if (con->sock == NULL)
  1267. goto out_connect;
  1268. spin_lock(&con->writequeue_lock);
  1269. for (;;) {
  1270. e = list_entry(con->writequeue.next, struct writequeue_entry,
  1271. list);
  1272. if ((struct list_head *) e == &con->writequeue)
  1273. break;
  1274. len = e->len;
  1275. offset = e->offset;
  1276. BUG_ON(len == 0 && e->users == 0);
  1277. spin_unlock(&con->writequeue_lock);
  1278. ret = 0;
  1279. if (len) {
  1280. ret = kernel_sendpage(con->sock, e->page, offset, len,
  1281. msg_flags);
  1282. if (ret == -EAGAIN || ret == 0) {
  1283. if (ret == -EAGAIN &&
  1284. test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) &&
  1285. !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
  1286. /* Notify TCP that we're limited by the
  1287. * application window size.
  1288. */
  1289. set_bit(SOCK_NOSPACE, &con->sock->flags);
  1290. con->sock->sk->sk_write_pending++;
  1291. }
  1292. cond_resched();
  1293. goto out;
  1294. } else if (ret < 0)
  1295. goto send_error;
  1296. }
  1297. /* Don't starve people filling buffers */
  1298. if (++count >= MAX_SEND_MSG_COUNT) {
  1299. cond_resched();
  1300. count = 0;
  1301. }
  1302. spin_lock(&con->writequeue_lock);
  1303. writequeue_entry_complete(e, ret);
  1304. }
  1305. spin_unlock(&con->writequeue_lock);
  1306. out:
  1307. mutex_unlock(&con->sock_mutex);
  1308. return;
  1309. send_error:
  1310. mutex_unlock(&con->sock_mutex);
  1311. close_connection(con, true, false, true);
  1312. /* Requeue the send work. When the work daemon runs again, it will try
  1313. a new connection, then call this function again. */
  1314. queue_work(send_workqueue, &con->swork);
  1315. return;
  1316. out_connect:
  1317. mutex_unlock(&con->sock_mutex);
  1318. queue_work(send_workqueue, &con->swork);
  1319. cond_resched();
  1320. }
  1321. static void clean_one_writequeue(struct connection *con)
  1322. {
  1323. struct writequeue_entry *e, *safe;
  1324. spin_lock(&con->writequeue_lock);
  1325. list_for_each_entry_safe(e, safe, &con->writequeue, list) {
  1326. list_del(&e->list);
  1327. free_entry(e);
  1328. }
  1329. spin_unlock(&con->writequeue_lock);
  1330. }
  1331. /* Called from recovery when it knows that a node has
  1332. left the cluster */
  1333. int dlm_lowcomms_close(int nodeid)
  1334. {
  1335. struct connection *con;
  1336. struct dlm_node_addr *na;
  1337. log_print("closing connection to node %d", nodeid);
  1338. con = nodeid2con(nodeid, 0);
  1339. if (con) {
  1340. set_bit(CF_CLOSE, &con->flags);
  1341. close_connection(con, true, true, true);
  1342. clean_one_writequeue(con);
  1343. }
  1344. spin_lock(&dlm_node_addrs_spin);
  1345. na = find_node_addr(nodeid);
  1346. if (na) {
  1347. list_del(&na->list);
  1348. while (na->addr_count--)
  1349. kfree(na->addr[na->addr_count]);
  1350. kfree(na);
  1351. }
  1352. spin_unlock(&dlm_node_addrs_spin);
  1353. return 0;
  1354. }
  1355. /* Receive workqueue function */
  1356. static void process_recv_sockets(struct work_struct *work)
  1357. {
  1358. struct connection *con = container_of(work, struct connection, rwork);
  1359. int err;
  1360. clear_bit(CF_READ_PENDING, &con->flags);
  1361. do {
  1362. err = con->rx_action(con);
  1363. } while (!err);
  1364. }
  1365. /* Send workqueue function */
  1366. static void process_send_sockets(struct work_struct *work)
  1367. {
  1368. struct connection *con = container_of(work, struct connection, swork);
  1369. clear_bit(CF_WRITE_PENDING, &con->flags);
  1370. if (con->sock == NULL) /* not mutex protected so check it inside too */
  1371. con->connect_action(con);
  1372. if (!list_empty(&con->writequeue))
  1373. send_to_sock(con);
  1374. }
  1375. /* Discard all entries on the write queues */
  1376. static void clean_writequeues(void)
  1377. {
  1378. foreach_conn(clean_one_writequeue);
  1379. }
  1380. static void work_stop(void)
  1381. {
  1382. destroy_workqueue(recv_workqueue);
  1383. destroy_workqueue(send_workqueue);
  1384. }
  1385. static int work_start(void)
  1386. {
  1387. recv_workqueue = alloc_workqueue("dlm_recv",
  1388. WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
  1389. if (!recv_workqueue) {
  1390. log_print("can't start dlm_recv");
  1391. return -ENOMEM;
  1392. }
  1393. send_workqueue = alloc_workqueue("dlm_send",
  1394. WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
  1395. if (!send_workqueue) {
  1396. log_print("can't start dlm_send");
  1397. destroy_workqueue(recv_workqueue);
  1398. return -ENOMEM;
  1399. }
  1400. return 0;
  1401. }
  1402. static void _stop_conn(struct connection *con, bool and_other)
  1403. {
  1404. mutex_lock(&con->sock_mutex);
  1405. set_bit(CF_CLOSE, &con->flags);
  1406. set_bit(CF_READ_PENDING, &con->flags);
  1407. set_bit(CF_WRITE_PENDING, &con->flags);
  1408. if (con->sock && con->sock->sk) {
  1409. write_lock_bh(&con->sock->sk->sk_callback_lock);
  1410. con->sock->sk->sk_user_data = NULL;
  1411. write_unlock_bh(&con->sock->sk->sk_callback_lock);
  1412. }
  1413. if (con->othercon && and_other)
  1414. _stop_conn(con->othercon, false);
  1415. mutex_unlock(&con->sock_mutex);
  1416. }
  1417. static void stop_conn(struct connection *con)
  1418. {
  1419. _stop_conn(con, true);
  1420. }
  1421. static void free_conn(struct connection *con)
  1422. {
  1423. close_connection(con, true, true, true);
  1424. if (con->othercon)
  1425. kmem_cache_free(con_cache, con->othercon);
  1426. hlist_del(&con->list);
  1427. kmem_cache_free(con_cache, con);
  1428. }
  1429. static void work_flush(void)
  1430. {
  1431. int ok;
  1432. int i;
  1433. struct hlist_node *n;
  1434. struct connection *con;
  1435. flush_workqueue(recv_workqueue);
  1436. flush_workqueue(send_workqueue);
  1437. do {
  1438. ok = 1;
  1439. foreach_conn(stop_conn);
  1440. flush_workqueue(recv_workqueue);
  1441. flush_workqueue(send_workqueue);
  1442. for (i = 0; i < CONN_HASH_SIZE && ok; i++) {
  1443. hlist_for_each_entry_safe(con, n,
  1444. &connection_hash[i], list) {
  1445. ok &= test_bit(CF_READ_PENDING, &con->flags);
  1446. ok &= test_bit(CF_WRITE_PENDING, &con->flags);
  1447. if (con->othercon) {
  1448. ok &= test_bit(CF_READ_PENDING,
  1449. &con->othercon->flags);
  1450. ok &= test_bit(CF_WRITE_PENDING,
  1451. &con->othercon->flags);
  1452. }
  1453. }
  1454. }
  1455. } while (!ok);
  1456. }
  1457. void dlm_lowcomms_stop(void)
  1458. {
  1459. /* Set all the flags to prevent any
  1460. socket activity.
  1461. */
  1462. mutex_lock(&connections_lock);
  1463. dlm_allow_conn = 0;
  1464. mutex_unlock(&connections_lock);
  1465. work_flush();
  1466. clean_writequeues();
  1467. foreach_conn(free_conn);
  1468. work_stop();
  1469. kmem_cache_destroy(con_cache);
  1470. }
  1471. int dlm_lowcomms_start(void)
  1472. {
  1473. int error = -EINVAL;
  1474. struct connection *con;
  1475. int i;
  1476. for (i = 0; i < CONN_HASH_SIZE; i++)
  1477. INIT_HLIST_HEAD(&connection_hash[i]);
  1478. init_local();
  1479. if (!dlm_local_count) {
  1480. error = -ENOTCONN;
  1481. log_print("no local IP address has been set");
  1482. goto fail;
  1483. }
  1484. error = -ENOMEM;
  1485. con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
  1486. __alignof__(struct connection), 0,
  1487. NULL);
  1488. if (!con_cache)
  1489. goto fail;
  1490. error = work_start();
  1491. if (error)
  1492. goto fail_destroy;
  1493. dlm_allow_conn = 1;
  1494. /* Start listening */
  1495. if (dlm_config.ci_protocol == 0)
  1496. error = tcp_listen_for_all();
  1497. else
  1498. error = sctp_listen_for_all();
  1499. if (error)
  1500. goto fail_unlisten;
  1501. return 0;
  1502. fail_unlisten:
  1503. dlm_allow_conn = 0;
  1504. con = nodeid2con(0,0);
  1505. if (con) {
  1506. close_connection(con, false, true, true);
  1507. kmem_cache_free(con_cache, con);
  1508. }
  1509. fail_destroy:
  1510. kmem_cache_destroy(con_cache);
  1511. fail:
  1512. return error;
  1513. }
  1514. void dlm_lowcomms_exit(void)
  1515. {
  1516. struct dlm_node_addr *na, *safe;
  1517. spin_lock(&dlm_node_addrs_spin);
  1518. list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) {
  1519. list_del(&na->list);
  1520. while (na->addr_count--)
  1521. kfree(na->addr[na->addr_count]);
  1522. kfree(na);
  1523. }
  1524. spin_unlock(&dlm_node_addrs_spin);
  1525. }