lowcomms.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  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/sctp/user.h>
  55. #include <net/ipv6.h>
  56. #include "dlm_internal.h"
  57. #include "lowcomms.h"
  58. #include "midcomms.h"
  59. #include "config.h"
  60. #define NEEDED_RMEM (4*1024*1024)
  61. #define CONN_HASH_SIZE 32
  62. /* Number of messages to send before rescheduling */
  63. #define MAX_SEND_MSG_COUNT 25
  64. struct cbuf {
  65. unsigned int base;
  66. unsigned int len;
  67. unsigned int mask;
  68. };
  69. static void cbuf_add(struct cbuf *cb, int n)
  70. {
  71. cb->len += n;
  72. }
  73. static int cbuf_data(struct cbuf *cb)
  74. {
  75. return ((cb->base + cb->len) & cb->mask);
  76. }
  77. static void cbuf_init(struct cbuf *cb, int size)
  78. {
  79. cb->base = cb->len = 0;
  80. cb->mask = size-1;
  81. }
  82. static void cbuf_eat(struct cbuf *cb, int n)
  83. {
  84. cb->len -= n;
  85. cb->base += n;
  86. cb->base &= cb->mask;
  87. }
  88. static bool cbuf_empty(struct cbuf *cb)
  89. {
  90. return cb->len == 0;
  91. }
  92. struct connection {
  93. struct socket *sock; /* NULL if not connected */
  94. uint32_t nodeid; /* So we know who we are in the list */
  95. struct mutex sock_mutex;
  96. unsigned long flags;
  97. #define CF_READ_PENDING 1
  98. #define CF_WRITE_PENDING 2
  99. #define CF_CONNECT_PENDING 3
  100. #define CF_INIT_PENDING 4
  101. #define CF_IS_OTHERCON 5
  102. #define CF_CLOSE 6
  103. #define CF_APP_LIMITED 7
  104. struct list_head writequeue; /* List of outgoing writequeue_entries */
  105. spinlock_t writequeue_lock;
  106. int (*rx_action) (struct connection *); /* What to do when active */
  107. void (*connect_action) (struct connection *); /* What to do to connect */
  108. struct page *rx_page;
  109. struct cbuf cb;
  110. int retries;
  111. #define MAX_CONNECT_RETRIES 3
  112. int sctp_assoc;
  113. struct hlist_node list;
  114. struct connection *othercon;
  115. struct work_struct rwork; /* Receive workqueue */
  116. struct work_struct swork; /* Send workqueue */
  117. };
  118. #define sock2con(x) ((struct connection *)(x)->sk_user_data)
  119. /* An entry waiting to be sent */
  120. struct writequeue_entry {
  121. struct list_head list;
  122. struct page *page;
  123. int offset;
  124. int len;
  125. int end;
  126. int users;
  127. struct connection *con;
  128. };
  129. static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
  130. static int dlm_local_count;
  131. static int dlm_allow_conn;
  132. /* Work queues */
  133. static struct workqueue_struct *recv_workqueue;
  134. static struct workqueue_struct *send_workqueue;
  135. static struct hlist_head connection_hash[CONN_HASH_SIZE];
  136. static DEFINE_MUTEX(connections_lock);
  137. static struct kmem_cache *con_cache;
  138. static void process_recv_sockets(struct work_struct *work);
  139. static void process_send_sockets(struct work_struct *work);
  140. /* This is deliberately very simple because most clusters have simple
  141. sequential nodeids, so we should be able to go straight to a connection
  142. struct in the array */
  143. static inline int nodeid_hash(int nodeid)
  144. {
  145. return nodeid & (CONN_HASH_SIZE-1);
  146. }
  147. static struct connection *__find_con(int nodeid)
  148. {
  149. int r;
  150. struct hlist_node *h;
  151. struct connection *con;
  152. r = nodeid_hash(nodeid);
  153. hlist_for_each_entry(con, h, &connection_hash[r], list) {
  154. if (con->nodeid == nodeid)
  155. return con;
  156. }
  157. return NULL;
  158. }
  159. /*
  160. * If 'allocation' is zero then we don't attempt to create a new
  161. * connection structure for this node.
  162. */
  163. static struct connection *__nodeid2con(int nodeid, gfp_t alloc)
  164. {
  165. struct connection *con = NULL;
  166. int r;
  167. con = __find_con(nodeid);
  168. if (con || !alloc)
  169. return con;
  170. con = kmem_cache_zalloc(con_cache, alloc);
  171. if (!con)
  172. return NULL;
  173. r = nodeid_hash(nodeid);
  174. hlist_add_head(&con->list, &connection_hash[r]);
  175. con->nodeid = nodeid;
  176. mutex_init(&con->sock_mutex);
  177. INIT_LIST_HEAD(&con->writequeue);
  178. spin_lock_init(&con->writequeue_lock);
  179. INIT_WORK(&con->swork, process_send_sockets);
  180. INIT_WORK(&con->rwork, process_recv_sockets);
  181. /* Setup action pointers for child sockets */
  182. if (con->nodeid) {
  183. struct connection *zerocon = __find_con(0);
  184. con->connect_action = zerocon->connect_action;
  185. if (!con->rx_action)
  186. con->rx_action = zerocon->rx_action;
  187. }
  188. return con;
  189. }
  190. /* Loop round all connections */
  191. static void foreach_conn(void (*conn_func)(struct connection *c))
  192. {
  193. int i;
  194. struct hlist_node *h, *n;
  195. struct connection *con;
  196. for (i = 0; i < CONN_HASH_SIZE; i++) {
  197. hlist_for_each_entry_safe(con, h, n, &connection_hash[i], list){
  198. conn_func(con);
  199. }
  200. }
  201. }
  202. static struct connection *nodeid2con(int nodeid, gfp_t allocation)
  203. {
  204. struct connection *con;
  205. mutex_lock(&connections_lock);
  206. con = __nodeid2con(nodeid, allocation);
  207. mutex_unlock(&connections_lock);
  208. return con;
  209. }
  210. /* This is a bit drastic, but only called when things go wrong */
  211. static struct connection *assoc2con(int assoc_id)
  212. {
  213. int i;
  214. struct hlist_node *h;
  215. struct connection *con;
  216. mutex_lock(&connections_lock);
  217. for (i = 0 ; i < CONN_HASH_SIZE; i++) {
  218. hlist_for_each_entry(con, h, &connection_hash[i], list) {
  219. if (con->sctp_assoc == assoc_id) {
  220. mutex_unlock(&connections_lock);
  221. return con;
  222. }
  223. }
  224. }
  225. mutex_unlock(&connections_lock);
  226. return NULL;
  227. }
  228. static int nodeid_to_addr(int nodeid, struct sockaddr *retaddr)
  229. {
  230. struct sockaddr_storage addr;
  231. int error;
  232. if (!dlm_local_count)
  233. return -1;
  234. error = dlm_nodeid_to_addr(nodeid, &addr);
  235. if (error)
  236. return error;
  237. if (dlm_local_addr[0]->ss_family == AF_INET) {
  238. struct sockaddr_in *in4 = (struct sockaddr_in *) &addr;
  239. struct sockaddr_in *ret4 = (struct sockaddr_in *) retaddr;
  240. ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
  241. } else {
  242. struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &addr;
  243. struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) retaddr;
  244. ret6->sin6_addr = in6->sin6_addr;
  245. }
  246. return 0;
  247. }
  248. /* Data available on socket or listen socket received a connect */
  249. static void lowcomms_data_ready(struct sock *sk, int count_unused)
  250. {
  251. struct connection *con = sock2con(sk);
  252. if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
  253. queue_work(recv_workqueue, &con->rwork);
  254. }
  255. static void lowcomms_write_space(struct sock *sk)
  256. {
  257. struct connection *con = sock2con(sk);
  258. if (!con)
  259. return;
  260. clear_bit(SOCK_NOSPACE, &con->sock->flags);
  261. if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
  262. con->sock->sk->sk_write_pending--;
  263. clear_bit(SOCK_ASYNC_NOSPACE, &con->sock->flags);
  264. }
  265. if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags))
  266. queue_work(send_workqueue, &con->swork);
  267. }
  268. static inline void lowcomms_connect_sock(struct connection *con)
  269. {
  270. if (test_bit(CF_CLOSE, &con->flags))
  271. return;
  272. if (!test_and_set_bit(CF_CONNECT_PENDING, &con->flags))
  273. queue_work(send_workqueue, &con->swork);
  274. }
  275. static void lowcomms_state_change(struct sock *sk)
  276. {
  277. if (sk->sk_state == TCP_ESTABLISHED)
  278. lowcomms_write_space(sk);
  279. }
  280. int dlm_lowcomms_connect_node(int nodeid)
  281. {
  282. struct connection *con;
  283. /* with sctp there's no connecting without sending */
  284. if (dlm_config.ci_protocol != 0)
  285. return 0;
  286. if (nodeid == dlm_our_nodeid())
  287. return 0;
  288. con = nodeid2con(nodeid, GFP_NOFS);
  289. if (!con)
  290. return -ENOMEM;
  291. lowcomms_connect_sock(con);
  292. return 0;
  293. }
  294. /* Make a socket active */
  295. static int add_sock(struct socket *sock, struct connection *con)
  296. {
  297. con->sock = sock;
  298. /* Install a data_ready callback */
  299. con->sock->sk->sk_data_ready = lowcomms_data_ready;
  300. con->sock->sk->sk_write_space = lowcomms_write_space;
  301. con->sock->sk->sk_state_change = lowcomms_state_change;
  302. con->sock->sk->sk_user_data = con;
  303. con->sock->sk->sk_allocation = GFP_NOFS;
  304. return 0;
  305. }
  306. /* Add the port number to an IPv6 or 4 sockaddr and return the address
  307. length */
  308. static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
  309. int *addr_len)
  310. {
  311. saddr->ss_family = dlm_local_addr[0]->ss_family;
  312. if (saddr->ss_family == AF_INET) {
  313. struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
  314. in4_addr->sin_port = cpu_to_be16(port);
  315. *addr_len = sizeof(struct sockaddr_in);
  316. memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
  317. } else {
  318. struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
  319. in6_addr->sin6_port = cpu_to_be16(port);
  320. *addr_len = sizeof(struct sockaddr_in6);
  321. }
  322. memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
  323. }
  324. /* Close a remote connection and tidy up */
  325. static void close_connection(struct connection *con, bool and_other)
  326. {
  327. mutex_lock(&con->sock_mutex);
  328. if (con->sock) {
  329. sock_release(con->sock);
  330. con->sock = NULL;
  331. }
  332. if (con->othercon && and_other) {
  333. /* Will only re-enter once. */
  334. close_connection(con->othercon, false);
  335. }
  336. if (con->rx_page) {
  337. __free_page(con->rx_page);
  338. con->rx_page = NULL;
  339. }
  340. con->retries = 0;
  341. mutex_unlock(&con->sock_mutex);
  342. }
  343. /* We only send shutdown messages to nodes that are not part of the cluster */
  344. static void sctp_send_shutdown(sctp_assoc_t associd)
  345. {
  346. static char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
  347. struct msghdr outmessage;
  348. struct cmsghdr *cmsg;
  349. struct sctp_sndrcvinfo *sinfo;
  350. int ret;
  351. struct connection *con;
  352. con = nodeid2con(0,0);
  353. BUG_ON(con == NULL);
  354. outmessage.msg_name = NULL;
  355. outmessage.msg_namelen = 0;
  356. outmessage.msg_control = outcmsg;
  357. outmessage.msg_controllen = sizeof(outcmsg);
  358. outmessage.msg_flags = MSG_EOR;
  359. cmsg = CMSG_FIRSTHDR(&outmessage);
  360. cmsg->cmsg_level = IPPROTO_SCTP;
  361. cmsg->cmsg_type = SCTP_SNDRCV;
  362. cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
  363. outmessage.msg_controllen = cmsg->cmsg_len;
  364. sinfo = CMSG_DATA(cmsg);
  365. memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
  366. sinfo->sinfo_flags |= MSG_EOF;
  367. sinfo->sinfo_assoc_id = associd;
  368. ret = kernel_sendmsg(con->sock, &outmessage, NULL, 0, 0);
  369. if (ret != 0)
  370. log_print("send EOF to node failed: %d", ret);
  371. }
  372. static void sctp_init_failed_foreach(struct connection *con)
  373. {
  374. con->sctp_assoc = 0;
  375. if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
  376. if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags))
  377. queue_work(send_workqueue, &con->swork);
  378. }
  379. }
  380. /* INIT failed but we don't know which node...
  381. restart INIT on all pending nodes */
  382. static void sctp_init_failed(void)
  383. {
  384. mutex_lock(&connections_lock);
  385. foreach_conn(sctp_init_failed_foreach);
  386. mutex_unlock(&connections_lock);
  387. }
  388. /* Something happened to an association */
  389. static void process_sctp_notification(struct connection *con,
  390. struct msghdr *msg, char *buf)
  391. {
  392. union sctp_notification *sn = (union sctp_notification *)buf;
  393. if (sn->sn_header.sn_type == SCTP_ASSOC_CHANGE) {
  394. switch (sn->sn_assoc_change.sac_state) {
  395. case SCTP_COMM_UP:
  396. case SCTP_RESTART:
  397. {
  398. /* Check that the new node is in the lockspace */
  399. struct sctp_prim prim;
  400. int nodeid;
  401. int prim_len, ret;
  402. int addr_len;
  403. struct connection *new_con;
  404. /*
  405. * We get this before any data for an association.
  406. * We verify that the node is in the cluster and
  407. * then peel off a socket for it.
  408. */
  409. if ((int)sn->sn_assoc_change.sac_assoc_id <= 0) {
  410. log_print("COMM_UP for invalid assoc ID %d",
  411. (int)sn->sn_assoc_change.sac_assoc_id);
  412. sctp_init_failed();
  413. return;
  414. }
  415. memset(&prim, 0, sizeof(struct sctp_prim));
  416. prim_len = sizeof(struct sctp_prim);
  417. prim.ssp_assoc_id = sn->sn_assoc_change.sac_assoc_id;
  418. ret = kernel_getsockopt(con->sock,
  419. IPPROTO_SCTP,
  420. SCTP_PRIMARY_ADDR,
  421. (char*)&prim,
  422. &prim_len);
  423. if (ret < 0) {
  424. log_print("getsockopt/sctp_primary_addr on "
  425. "new assoc %d failed : %d",
  426. (int)sn->sn_assoc_change.sac_assoc_id,
  427. ret);
  428. /* Retry INIT later */
  429. new_con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
  430. if (new_con)
  431. clear_bit(CF_CONNECT_PENDING, &con->flags);
  432. return;
  433. }
  434. make_sockaddr(&prim.ssp_addr, 0, &addr_len);
  435. if (dlm_addr_to_nodeid(&prim.ssp_addr, &nodeid)) {
  436. unsigned char *b=(unsigned char *)&prim.ssp_addr;
  437. log_print("reject connect from unknown addr");
  438. print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
  439. b, sizeof(struct sockaddr_storage));
  440. sctp_send_shutdown(prim.ssp_assoc_id);
  441. return;
  442. }
  443. new_con = nodeid2con(nodeid, GFP_NOFS);
  444. if (!new_con)
  445. return;
  446. /* Peel off a new sock */
  447. sctp_lock_sock(con->sock->sk);
  448. ret = sctp_do_peeloff(con->sock->sk,
  449. sn->sn_assoc_change.sac_assoc_id,
  450. &new_con->sock);
  451. sctp_release_sock(con->sock->sk);
  452. if (ret < 0) {
  453. log_print("Can't peel off a socket for "
  454. "connection %d to node %d: err=%d",
  455. (int)sn->sn_assoc_change.sac_assoc_id,
  456. nodeid, ret);
  457. return;
  458. }
  459. add_sock(new_con->sock, new_con);
  460. log_print("connecting to %d sctp association %d",
  461. nodeid, (int)sn->sn_assoc_change.sac_assoc_id);
  462. /* Send any pending writes */
  463. clear_bit(CF_CONNECT_PENDING, &new_con->flags);
  464. clear_bit(CF_INIT_PENDING, &con->flags);
  465. if (!test_and_set_bit(CF_WRITE_PENDING, &new_con->flags)) {
  466. queue_work(send_workqueue, &new_con->swork);
  467. }
  468. if (!test_and_set_bit(CF_READ_PENDING, &new_con->flags))
  469. queue_work(recv_workqueue, &new_con->rwork);
  470. }
  471. break;
  472. case SCTP_COMM_LOST:
  473. case SCTP_SHUTDOWN_COMP:
  474. {
  475. con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
  476. if (con) {
  477. con->sctp_assoc = 0;
  478. }
  479. }
  480. break;
  481. /* We don't know which INIT failed, so clear the PENDING flags
  482. * on them all. if assoc_id is zero then it will then try
  483. * again */
  484. case SCTP_CANT_STR_ASSOC:
  485. {
  486. log_print("Can't start SCTP association - retrying");
  487. sctp_init_failed();
  488. }
  489. break;
  490. default:
  491. log_print("unexpected SCTP assoc change id=%d state=%d",
  492. (int)sn->sn_assoc_change.sac_assoc_id,
  493. sn->sn_assoc_change.sac_state);
  494. }
  495. }
  496. }
  497. /* Data received from remote end */
  498. static int receive_from_sock(struct connection *con)
  499. {
  500. int ret = 0;
  501. struct msghdr msg = {};
  502. struct kvec iov[2];
  503. unsigned len;
  504. int r;
  505. int call_again_soon = 0;
  506. int nvec;
  507. char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
  508. mutex_lock(&con->sock_mutex);
  509. if (con->sock == NULL) {
  510. ret = -EAGAIN;
  511. goto out_close;
  512. }
  513. if (con->rx_page == NULL) {
  514. /*
  515. * This doesn't need to be atomic, but I think it should
  516. * improve performance if it is.
  517. */
  518. con->rx_page = alloc_page(GFP_ATOMIC);
  519. if (con->rx_page == NULL)
  520. goto out_resched;
  521. cbuf_init(&con->cb, PAGE_CACHE_SIZE);
  522. }
  523. /* Only SCTP needs these really */
  524. memset(&incmsg, 0, sizeof(incmsg));
  525. msg.msg_control = incmsg;
  526. msg.msg_controllen = sizeof(incmsg);
  527. /*
  528. * iov[0] is the bit of the circular buffer between the current end
  529. * point (cb.base + cb.len) and the end of the buffer.
  530. */
  531. iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
  532. iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
  533. iov[1].iov_len = 0;
  534. nvec = 1;
  535. /*
  536. * iov[1] is the bit of the circular buffer between the start of the
  537. * buffer and the start of the currently used section (cb.base)
  538. */
  539. if (cbuf_data(&con->cb) >= con->cb.base) {
  540. iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb);
  541. iov[1].iov_len = con->cb.base;
  542. iov[1].iov_base = page_address(con->rx_page);
  543. nvec = 2;
  544. }
  545. len = iov[0].iov_len + iov[1].iov_len;
  546. r = ret = kernel_recvmsg(con->sock, &msg, iov, nvec, len,
  547. MSG_DONTWAIT | MSG_NOSIGNAL);
  548. if (ret <= 0)
  549. goto out_close;
  550. /* Process SCTP notifications */
  551. if (msg.msg_flags & MSG_NOTIFICATION) {
  552. msg.msg_control = incmsg;
  553. msg.msg_controllen = sizeof(incmsg);
  554. process_sctp_notification(con, &msg,
  555. page_address(con->rx_page) + con->cb.base);
  556. mutex_unlock(&con->sock_mutex);
  557. return 0;
  558. }
  559. BUG_ON(con->nodeid == 0);
  560. if (ret == len)
  561. call_again_soon = 1;
  562. cbuf_add(&con->cb, ret);
  563. ret = dlm_process_incoming_buffer(con->nodeid,
  564. page_address(con->rx_page),
  565. con->cb.base, con->cb.len,
  566. PAGE_CACHE_SIZE);
  567. if (ret == -EBADMSG) {
  568. log_print("lowcomms: addr=%p, base=%u, len=%u, "
  569. "iov_len=%u, iov_base[0]=%p, read=%d",
  570. page_address(con->rx_page), con->cb.base, con->cb.len,
  571. len, iov[0].iov_base, r);
  572. }
  573. if (ret < 0)
  574. goto out_close;
  575. cbuf_eat(&con->cb, ret);
  576. if (cbuf_empty(&con->cb) && !call_again_soon) {
  577. __free_page(con->rx_page);
  578. con->rx_page = NULL;
  579. }
  580. if (call_again_soon)
  581. goto out_resched;
  582. mutex_unlock(&con->sock_mutex);
  583. return 0;
  584. out_resched:
  585. if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
  586. queue_work(recv_workqueue, &con->rwork);
  587. mutex_unlock(&con->sock_mutex);
  588. return -EAGAIN;
  589. out_close:
  590. mutex_unlock(&con->sock_mutex);
  591. if (ret != -EAGAIN) {
  592. close_connection(con, false);
  593. /* Reconnect when there is something to send */
  594. }
  595. /* Don't return success if we really got EOF */
  596. if (ret == 0)
  597. ret = -EAGAIN;
  598. return ret;
  599. }
  600. /* Listening socket is busy, accept a connection */
  601. static int tcp_accept_from_sock(struct connection *con)
  602. {
  603. int result;
  604. struct sockaddr_storage peeraddr;
  605. struct socket *newsock;
  606. int len;
  607. int nodeid;
  608. struct connection *newcon;
  609. struct connection *addcon;
  610. mutex_lock(&connections_lock);
  611. if (!dlm_allow_conn) {
  612. mutex_unlock(&connections_lock);
  613. return -1;
  614. }
  615. mutex_unlock(&connections_lock);
  616. memset(&peeraddr, 0, sizeof(peeraddr));
  617. result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
  618. IPPROTO_TCP, &newsock);
  619. if (result < 0)
  620. return -ENOMEM;
  621. mutex_lock_nested(&con->sock_mutex, 0);
  622. result = -ENOTCONN;
  623. if (con->sock == NULL)
  624. goto accept_err;
  625. newsock->type = con->sock->type;
  626. newsock->ops = con->sock->ops;
  627. result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK);
  628. if (result < 0)
  629. goto accept_err;
  630. /* Get the connected socket's peer */
  631. memset(&peeraddr, 0, sizeof(peeraddr));
  632. if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr,
  633. &len, 2)) {
  634. result = -ECONNABORTED;
  635. goto accept_err;
  636. }
  637. /* Get the new node's NODEID */
  638. make_sockaddr(&peeraddr, 0, &len);
  639. if (dlm_addr_to_nodeid(&peeraddr, &nodeid)) {
  640. unsigned char *b=(unsigned char *)&peeraddr;
  641. log_print("connect from non cluster node");
  642. print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
  643. b, sizeof(struct sockaddr_storage));
  644. sock_release(newsock);
  645. mutex_unlock(&con->sock_mutex);
  646. return -1;
  647. }
  648. log_print("got connection from %d", nodeid);
  649. /* Check to see if we already have a connection to this node. This
  650. * could happen if the two nodes initiate a connection at roughly
  651. * the same time and the connections cross on the wire.
  652. * In this case we store the incoming one in "othercon"
  653. */
  654. newcon = nodeid2con(nodeid, GFP_NOFS);
  655. if (!newcon) {
  656. result = -ENOMEM;
  657. goto accept_err;
  658. }
  659. mutex_lock_nested(&newcon->sock_mutex, 1);
  660. if (newcon->sock) {
  661. struct connection *othercon = newcon->othercon;
  662. if (!othercon) {
  663. othercon = kmem_cache_zalloc(con_cache, GFP_NOFS);
  664. if (!othercon) {
  665. log_print("failed to allocate incoming socket");
  666. mutex_unlock(&newcon->sock_mutex);
  667. result = -ENOMEM;
  668. goto accept_err;
  669. }
  670. othercon->nodeid = nodeid;
  671. othercon->rx_action = receive_from_sock;
  672. mutex_init(&othercon->sock_mutex);
  673. INIT_WORK(&othercon->swork, process_send_sockets);
  674. INIT_WORK(&othercon->rwork, process_recv_sockets);
  675. set_bit(CF_IS_OTHERCON, &othercon->flags);
  676. }
  677. if (!othercon->sock) {
  678. newcon->othercon = othercon;
  679. othercon->sock = newsock;
  680. newsock->sk->sk_user_data = othercon;
  681. add_sock(newsock, othercon);
  682. addcon = othercon;
  683. }
  684. else {
  685. printk("Extra connection from node %d attempted\n", nodeid);
  686. result = -EAGAIN;
  687. mutex_unlock(&newcon->sock_mutex);
  688. goto accept_err;
  689. }
  690. }
  691. else {
  692. newsock->sk->sk_user_data = newcon;
  693. newcon->rx_action = receive_from_sock;
  694. add_sock(newsock, newcon);
  695. addcon = newcon;
  696. }
  697. mutex_unlock(&newcon->sock_mutex);
  698. /*
  699. * Add it to the active queue in case we got data
  700. * between processing the accept adding the socket
  701. * to the read_sockets list
  702. */
  703. if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
  704. queue_work(recv_workqueue, &addcon->rwork);
  705. mutex_unlock(&con->sock_mutex);
  706. return 0;
  707. accept_err:
  708. mutex_unlock(&con->sock_mutex);
  709. sock_release(newsock);
  710. if (result != -EAGAIN)
  711. log_print("error accepting connection from node: %d", result);
  712. return result;
  713. }
  714. static void free_entry(struct writequeue_entry *e)
  715. {
  716. __free_page(e->page);
  717. kfree(e);
  718. }
  719. /* Initiate an SCTP association.
  720. This is a special case of send_to_sock() in that we don't yet have a
  721. peeled-off socket for this association, so we use the listening socket
  722. and add the primary IP address of the remote node.
  723. */
  724. static void sctp_init_assoc(struct connection *con)
  725. {
  726. struct sockaddr_storage rem_addr;
  727. char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
  728. struct msghdr outmessage;
  729. struct cmsghdr *cmsg;
  730. struct sctp_sndrcvinfo *sinfo;
  731. struct connection *base_con;
  732. struct writequeue_entry *e;
  733. int len, offset;
  734. int ret;
  735. int addrlen;
  736. struct kvec iov[1];
  737. if (test_and_set_bit(CF_INIT_PENDING, &con->flags))
  738. return;
  739. if (con->retries++ > MAX_CONNECT_RETRIES)
  740. return;
  741. if (nodeid_to_addr(con->nodeid, (struct sockaddr *)&rem_addr)) {
  742. log_print("no address for nodeid %d", con->nodeid);
  743. return;
  744. }
  745. base_con = nodeid2con(0, 0);
  746. BUG_ON(base_con == NULL);
  747. make_sockaddr(&rem_addr, dlm_config.ci_tcp_port, &addrlen);
  748. outmessage.msg_name = &rem_addr;
  749. outmessage.msg_namelen = addrlen;
  750. outmessage.msg_control = outcmsg;
  751. outmessage.msg_controllen = sizeof(outcmsg);
  752. outmessage.msg_flags = MSG_EOR;
  753. spin_lock(&con->writequeue_lock);
  754. if (list_empty(&con->writequeue)) {
  755. spin_unlock(&con->writequeue_lock);
  756. log_print("writequeue empty for nodeid %d", con->nodeid);
  757. return;
  758. }
  759. e = list_first_entry(&con->writequeue, struct writequeue_entry, list);
  760. len = e->len;
  761. offset = e->offset;
  762. spin_unlock(&con->writequeue_lock);
  763. /* Send the first block off the write queue */
  764. iov[0].iov_base = page_address(e->page)+offset;
  765. iov[0].iov_len = len;
  766. cmsg = CMSG_FIRSTHDR(&outmessage);
  767. cmsg->cmsg_level = IPPROTO_SCTP;
  768. cmsg->cmsg_type = SCTP_SNDRCV;
  769. cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
  770. sinfo = CMSG_DATA(cmsg);
  771. memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
  772. sinfo->sinfo_ppid = cpu_to_le32(dlm_our_nodeid());
  773. outmessage.msg_controllen = cmsg->cmsg_len;
  774. ret = kernel_sendmsg(base_con->sock, &outmessage, iov, 1, len);
  775. if (ret < 0) {
  776. log_print("Send first packet to node %d failed: %d",
  777. con->nodeid, ret);
  778. /* Try again later */
  779. clear_bit(CF_CONNECT_PENDING, &con->flags);
  780. clear_bit(CF_INIT_PENDING, &con->flags);
  781. }
  782. else {
  783. spin_lock(&con->writequeue_lock);
  784. e->offset += ret;
  785. e->len -= ret;
  786. if (e->len == 0 && e->users == 0) {
  787. list_del(&e->list);
  788. free_entry(e);
  789. }
  790. spin_unlock(&con->writequeue_lock);
  791. }
  792. }
  793. /* Connect a new socket to its peer */
  794. static void tcp_connect_to_sock(struct connection *con)
  795. {
  796. int result = -EHOSTUNREACH;
  797. struct sockaddr_storage saddr, src_addr;
  798. int addr_len;
  799. struct socket *sock = NULL;
  800. int one = 1;
  801. if (con->nodeid == 0) {
  802. log_print("attempt to connect sock 0 foiled");
  803. return;
  804. }
  805. mutex_lock(&con->sock_mutex);
  806. if (con->retries++ > MAX_CONNECT_RETRIES)
  807. goto out;
  808. /* Some odd races can cause double-connects, ignore them */
  809. if (con->sock) {
  810. result = 0;
  811. goto out;
  812. }
  813. /* Create a socket to communicate with */
  814. result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
  815. IPPROTO_TCP, &sock);
  816. if (result < 0)
  817. goto out_err;
  818. memset(&saddr, 0, sizeof(saddr));
  819. if (dlm_nodeid_to_addr(con->nodeid, &saddr))
  820. goto out_err;
  821. sock->sk->sk_user_data = con;
  822. con->rx_action = receive_from_sock;
  823. con->connect_action = tcp_connect_to_sock;
  824. add_sock(sock, con);
  825. /* Bind to our cluster-known address connecting to avoid
  826. routing problems */
  827. memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
  828. make_sockaddr(&src_addr, 0, &addr_len);
  829. result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
  830. addr_len);
  831. if (result < 0) {
  832. log_print("could not bind for connect: %d", result);
  833. /* This *may* not indicate a critical error */
  834. }
  835. make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
  836. log_print("connecting to %d", con->nodeid);
  837. /* Turn off Nagle's algorithm */
  838. kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
  839. sizeof(one));
  840. result =
  841. sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
  842. O_NONBLOCK);
  843. if (result == -EINPROGRESS)
  844. result = 0;
  845. if (result == 0)
  846. goto out;
  847. out_err:
  848. if (con->sock) {
  849. sock_release(con->sock);
  850. con->sock = NULL;
  851. } else if (sock) {
  852. sock_release(sock);
  853. }
  854. /*
  855. * Some errors are fatal and this list might need adjusting. For other
  856. * errors we try again until the max number of retries is reached.
  857. */
  858. if (result != -EHOSTUNREACH && result != -ENETUNREACH &&
  859. result != -ENETDOWN && result != -EINVAL
  860. && result != -EPROTONOSUPPORT) {
  861. lowcomms_connect_sock(con);
  862. result = 0;
  863. }
  864. out:
  865. mutex_unlock(&con->sock_mutex);
  866. return;
  867. }
  868. static struct socket *tcp_create_listen_sock(struct connection *con,
  869. struct sockaddr_storage *saddr)
  870. {
  871. struct socket *sock = NULL;
  872. int result = 0;
  873. int one = 1;
  874. int addr_len;
  875. if (dlm_local_addr[0]->ss_family == AF_INET)
  876. addr_len = sizeof(struct sockaddr_in);
  877. else
  878. addr_len = sizeof(struct sockaddr_in6);
  879. /* Create a socket to communicate with */
  880. result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
  881. IPPROTO_TCP, &sock);
  882. if (result < 0) {
  883. log_print("Can't create listening comms socket");
  884. goto create_out;
  885. }
  886. /* Turn off Nagle's algorithm */
  887. kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
  888. sizeof(one));
  889. result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  890. (char *)&one, sizeof(one));
  891. if (result < 0) {
  892. log_print("Failed to set SO_REUSEADDR on socket: %d", result);
  893. }
  894. sock->sk->sk_user_data = con;
  895. con->rx_action = tcp_accept_from_sock;
  896. con->connect_action = tcp_connect_to_sock;
  897. con->sock = sock;
  898. /* Bind to our port */
  899. make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
  900. result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
  901. if (result < 0) {
  902. log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
  903. sock_release(sock);
  904. sock = NULL;
  905. con->sock = NULL;
  906. goto create_out;
  907. }
  908. result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
  909. (char *)&one, sizeof(one));
  910. if (result < 0) {
  911. log_print("Set keepalive failed: %d", result);
  912. }
  913. result = sock->ops->listen(sock, 5);
  914. if (result < 0) {
  915. log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
  916. sock_release(sock);
  917. sock = NULL;
  918. goto create_out;
  919. }
  920. create_out:
  921. return sock;
  922. }
  923. /* Get local addresses */
  924. static void init_local(void)
  925. {
  926. struct sockaddr_storage sas, *addr;
  927. int i;
  928. dlm_local_count = 0;
  929. for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
  930. if (dlm_our_addr(&sas, i))
  931. break;
  932. addr = kmalloc(sizeof(*addr), GFP_NOFS);
  933. if (!addr)
  934. break;
  935. memcpy(addr, &sas, sizeof(*addr));
  936. dlm_local_addr[dlm_local_count++] = addr;
  937. }
  938. }
  939. /* Bind to an IP address. SCTP allows multiple address so it can do
  940. multi-homing */
  941. static int add_sctp_bind_addr(struct connection *sctp_con,
  942. struct sockaddr_storage *addr,
  943. int addr_len, int num)
  944. {
  945. int result = 0;
  946. if (num == 1)
  947. result = kernel_bind(sctp_con->sock,
  948. (struct sockaddr *) addr,
  949. addr_len);
  950. else
  951. result = kernel_setsockopt(sctp_con->sock, SOL_SCTP,
  952. SCTP_SOCKOPT_BINDX_ADD,
  953. (char *)addr, addr_len);
  954. if (result < 0)
  955. log_print("Can't bind to port %d addr number %d",
  956. dlm_config.ci_tcp_port, num);
  957. return result;
  958. }
  959. /* Initialise SCTP socket and bind to all interfaces */
  960. static int sctp_listen_for_all(void)
  961. {
  962. struct socket *sock = NULL;
  963. struct sockaddr_storage localaddr;
  964. struct sctp_event_subscribe subscribe;
  965. int result = -EINVAL, num = 1, i, addr_len;
  966. struct connection *con = nodeid2con(0, GFP_NOFS);
  967. int bufsize = NEEDED_RMEM;
  968. if (!con)
  969. return -ENOMEM;
  970. log_print("Using SCTP for communications");
  971. result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_SEQPACKET,
  972. IPPROTO_SCTP, &sock);
  973. if (result < 0) {
  974. log_print("Can't create comms socket, check SCTP is loaded");
  975. goto out;
  976. }
  977. /* Listen for events */
  978. memset(&subscribe, 0, sizeof(subscribe));
  979. subscribe.sctp_data_io_event = 1;
  980. subscribe.sctp_association_event = 1;
  981. subscribe.sctp_send_failure_event = 1;
  982. subscribe.sctp_shutdown_event = 1;
  983. subscribe.sctp_partial_delivery_event = 1;
  984. result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUFFORCE,
  985. (char *)&bufsize, sizeof(bufsize));
  986. if (result)
  987. log_print("Error increasing buffer space on socket %d", result);
  988. result = kernel_setsockopt(sock, SOL_SCTP, SCTP_EVENTS,
  989. (char *)&subscribe, sizeof(subscribe));
  990. if (result < 0) {
  991. log_print("Failed to set SCTP_EVENTS on socket: result=%d",
  992. result);
  993. goto create_delsock;
  994. }
  995. /* Init con struct */
  996. sock->sk->sk_user_data = con;
  997. con->sock = sock;
  998. con->sock->sk->sk_data_ready = lowcomms_data_ready;
  999. con->rx_action = receive_from_sock;
  1000. con->connect_action = sctp_init_assoc;
  1001. /* Bind to all interfaces. */
  1002. for (i = 0; i < dlm_local_count; i++) {
  1003. memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
  1004. make_sockaddr(&localaddr, dlm_config.ci_tcp_port, &addr_len);
  1005. result = add_sctp_bind_addr(con, &localaddr, addr_len, num);
  1006. if (result)
  1007. goto create_delsock;
  1008. ++num;
  1009. }
  1010. result = sock->ops->listen(sock, 5);
  1011. if (result < 0) {
  1012. log_print("Can't set socket listening");
  1013. goto create_delsock;
  1014. }
  1015. return 0;
  1016. create_delsock:
  1017. sock_release(sock);
  1018. con->sock = NULL;
  1019. out:
  1020. return result;
  1021. }
  1022. static int tcp_listen_for_all(void)
  1023. {
  1024. struct socket *sock = NULL;
  1025. struct connection *con = nodeid2con(0, GFP_NOFS);
  1026. int result = -EINVAL;
  1027. if (!con)
  1028. return -ENOMEM;
  1029. /* We don't support multi-homed hosts */
  1030. if (dlm_local_addr[1] != NULL) {
  1031. log_print("TCP protocol can't handle multi-homed hosts, "
  1032. "try SCTP");
  1033. return -EINVAL;
  1034. }
  1035. log_print("Using TCP for communications");
  1036. sock = tcp_create_listen_sock(con, dlm_local_addr[0]);
  1037. if (sock) {
  1038. add_sock(sock, con);
  1039. result = 0;
  1040. }
  1041. else {
  1042. result = -EADDRINUSE;
  1043. }
  1044. return result;
  1045. }
  1046. static struct writequeue_entry *new_writequeue_entry(struct connection *con,
  1047. gfp_t allocation)
  1048. {
  1049. struct writequeue_entry *entry;
  1050. entry = kmalloc(sizeof(struct writequeue_entry), allocation);
  1051. if (!entry)
  1052. return NULL;
  1053. entry->page = alloc_page(allocation);
  1054. if (!entry->page) {
  1055. kfree(entry);
  1056. return NULL;
  1057. }
  1058. entry->offset = 0;
  1059. entry->len = 0;
  1060. entry->end = 0;
  1061. entry->users = 0;
  1062. entry->con = con;
  1063. return entry;
  1064. }
  1065. void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
  1066. {
  1067. struct connection *con;
  1068. struct writequeue_entry *e;
  1069. int offset = 0;
  1070. int users = 0;
  1071. con = nodeid2con(nodeid, allocation);
  1072. if (!con)
  1073. return NULL;
  1074. spin_lock(&con->writequeue_lock);
  1075. e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
  1076. if ((&e->list == &con->writequeue) ||
  1077. (PAGE_CACHE_SIZE - e->end < len)) {
  1078. e = NULL;
  1079. } else {
  1080. offset = e->end;
  1081. e->end += len;
  1082. users = e->users++;
  1083. }
  1084. spin_unlock(&con->writequeue_lock);
  1085. if (e) {
  1086. got_one:
  1087. *ppc = page_address(e->page) + offset;
  1088. return e;
  1089. }
  1090. e = new_writequeue_entry(con, allocation);
  1091. if (e) {
  1092. spin_lock(&con->writequeue_lock);
  1093. offset = e->end;
  1094. e->end += len;
  1095. users = e->users++;
  1096. list_add_tail(&e->list, &con->writequeue);
  1097. spin_unlock(&con->writequeue_lock);
  1098. goto got_one;
  1099. }
  1100. return NULL;
  1101. }
  1102. void dlm_lowcomms_commit_buffer(void *mh)
  1103. {
  1104. struct writequeue_entry *e = (struct writequeue_entry *)mh;
  1105. struct connection *con = e->con;
  1106. int users;
  1107. spin_lock(&con->writequeue_lock);
  1108. users = --e->users;
  1109. if (users)
  1110. goto out;
  1111. e->len = e->end - e->offset;
  1112. spin_unlock(&con->writequeue_lock);
  1113. if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
  1114. queue_work(send_workqueue, &con->swork);
  1115. }
  1116. return;
  1117. out:
  1118. spin_unlock(&con->writequeue_lock);
  1119. return;
  1120. }
  1121. /* Send a message */
  1122. static void send_to_sock(struct connection *con)
  1123. {
  1124. int ret = 0;
  1125. const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
  1126. struct writequeue_entry *e;
  1127. int len, offset;
  1128. int count = 0;
  1129. mutex_lock(&con->sock_mutex);
  1130. if (con->sock == NULL)
  1131. goto out_connect;
  1132. spin_lock(&con->writequeue_lock);
  1133. for (;;) {
  1134. e = list_entry(con->writequeue.next, struct writequeue_entry,
  1135. list);
  1136. if ((struct list_head *) e == &con->writequeue)
  1137. break;
  1138. len = e->len;
  1139. offset = e->offset;
  1140. BUG_ON(len == 0 && e->users == 0);
  1141. spin_unlock(&con->writequeue_lock);
  1142. ret = 0;
  1143. if (len) {
  1144. ret = kernel_sendpage(con->sock, e->page, offset, len,
  1145. msg_flags);
  1146. if (ret == -EAGAIN || ret == 0) {
  1147. if (ret == -EAGAIN &&
  1148. test_bit(SOCK_ASYNC_NOSPACE, &con->sock->flags) &&
  1149. !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
  1150. /* Notify TCP that we're limited by the
  1151. * application window size.
  1152. */
  1153. set_bit(SOCK_NOSPACE, &con->sock->flags);
  1154. con->sock->sk->sk_write_pending++;
  1155. }
  1156. cond_resched();
  1157. goto out;
  1158. }
  1159. if (ret <= 0)
  1160. goto send_error;
  1161. }
  1162. /* Don't starve people filling buffers */
  1163. if (++count >= MAX_SEND_MSG_COUNT) {
  1164. cond_resched();
  1165. count = 0;
  1166. }
  1167. spin_lock(&con->writequeue_lock);
  1168. e->offset += ret;
  1169. e->len -= ret;
  1170. if (e->len == 0 && e->users == 0) {
  1171. list_del(&e->list);
  1172. free_entry(e);
  1173. continue;
  1174. }
  1175. }
  1176. spin_unlock(&con->writequeue_lock);
  1177. out:
  1178. mutex_unlock(&con->sock_mutex);
  1179. return;
  1180. send_error:
  1181. mutex_unlock(&con->sock_mutex);
  1182. close_connection(con, false);
  1183. lowcomms_connect_sock(con);
  1184. return;
  1185. out_connect:
  1186. mutex_unlock(&con->sock_mutex);
  1187. if (!test_bit(CF_INIT_PENDING, &con->flags))
  1188. lowcomms_connect_sock(con);
  1189. return;
  1190. }
  1191. static void clean_one_writequeue(struct connection *con)
  1192. {
  1193. struct writequeue_entry *e, *safe;
  1194. spin_lock(&con->writequeue_lock);
  1195. list_for_each_entry_safe(e, safe, &con->writequeue, list) {
  1196. list_del(&e->list);
  1197. free_entry(e);
  1198. }
  1199. spin_unlock(&con->writequeue_lock);
  1200. }
  1201. /* Called from recovery when it knows that a node has
  1202. left the cluster */
  1203. int dlm_lowcomms_close(int nodeid)
  1204. {
  1205. struct connection *con;
  1206. log_print("closing connection to node %d", nodeid);
  1207. con = nodeid2con(nodeid, 0);
  1208. if (con) {
  1209. clear_bit(CF_CONNECT_PENDING, &con->flags);
  1210. clear_bit(CF_WRITE_PENDING, &con->flags);
  1211. set_bit(CF_CLOSE, &con->flags);
  1212. if (cancel_work_sync(&con->swork))
  1213. log_print("canceled swork for node %d", nodeid);
  1214. if (cancel_work_sync(&con->rwork))
  1215. log_print("canceled rwork for node %d", nodeid);
  1216. clean_one_writequeue(con);
  1217. close_connection(con, true);
  1218. }
  1219. return 0;
  1220. }
  1221. /* Receive workqueue function */
  1222. static void process_recv_sockets(struct work_struct *work)
  1223. {
  1224. struct connection *con = container_of(work, struct connection, rwork);
  1225. int err;
  1226. clear_bit(CF_READ_PENDING, &con->flags);
  1227. do {
  1228. err = con->rx_action(con);
  1229. } while (!err);
  1230. }
  1231. /* Send workqueue function */
  1232. static void process_send_sockets(struct work_struct *work)
  1233. {
  1234. struct connection *con = container_of(work, struct connection, swork);
  1235. if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
  1236. con->connect_action(con);
  1237. set_bit(CF_WRITE_PENDING, &con->flags);
  1238. }
  1239. if (test_and_clear_bit(CF_WRITE_PENDING, &con->flags))
  1240. send_to_sock(con);
  1241. }
  1242. /* Discard all entries on the write queues */
  1243. static void clean_writequeues(void)
  1244. {
  1245. foreach_conn(clean_one_writequeue);
  1246. }
  1247. static void work_stop(void)
  1248. {
  1249. destroy_workqueue(recv_workqueue);
  1250. destroy_workqueue(send_workqueue);
  1251. }
  1252. static int work_start(void)
  1253. {
  1254. recv_workqueue = alloc_workqueue("dlm_recv",
  1255. WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
  1256. if (!recv_workqueue) {
  1257. log_print("can't start dlm_recv");
  1258. return -ENOMEM;
  1259. }
  1260. send_workqueue = alloc_workqueue("dlm_send",
  1261. WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
  1262. if (!send_workqueue) {
  1263. log_print("can't start dlm_send");
  1264. destroy_workqueue(recv_workqueue);
  1265. return -ENOMEM;
  1266. }
  1267. return 0;
  1268. }
  1269. static void stop_conn(struct connection *con)
  1270. {
  1271. con->flags |= 0x0F;
  1272. if (con->sock && con->sock->sk)
  1273. con->sock->sk->sk_user_data = NULL;
  1274. }
  1275. static void free_conn(struct connection *con)
  1276. {
  1277. close_connection(con, true);
  1278. if (con->othercon)
  1279. kmem_cache_free(con_cache, con->othercon);
  1280. hlist_del(&con->list);
  1281. kmem_cache_free(con_cache, con);
  1282. }
  1283. void dlm_lowcomms_stop(void)
  1284. {
  1285. /* Set all the flags to prevent any
  1286. socket activity.
  1287. */
  1288. mutex_lock(&connections_lock);
  1289. dlm_allow_conn = 0;
  1290. foreach_conn(stop_conn);
  1291. mutex_unlock(&connections_lock);
  1292. work_stop();
  1293. mutex_lock(&connections_lock);
  1294. clean_writequeues();
  1295. foreach_conn(free_conn);
  1296. mutex_unlock(&connections_lock);
  1297. kmem_cache_destroy(con_cache);
  1298. }
  1299. int dlm_lowcomms_start(void)
  1300. {
  1301. int error = -EINVAL;
  1302. struct connection *con;
  1303. int i;
  1304. for (i = 0; i < CONN_HASH_SIZE; i++)
  1305. INIT_HLIST_HEAD(&connection_hash[i]);
  1306. init_local();
  1307. if (!dlm_local_count) {
  1308. error = -ENOTCONN;
  1309. log_print("no local IP address has been set");
  1310. goto fail;
  1311. }
  1312. error = -ENOMEM;
  1313. con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
  1314. __alignof__(struct connection), 0,
  1315. NULL);
  1316. if (!con_cache)
  1317. goto fail;
  1318. error = work_start();
  1319. if (error)
  1320. goto fail_destroy;
  1321. dlm_allow_conn = 1;
  1322. /* Start listening */
  1323. if (dlm_config.ci_protocol == 0)
  1324. error = tcp_listen_for_all();
  1325. else
  1326. error = sctp_listen_for_all();
  1327. if (error)
  1328. goto fail_unlisten;
  1329. return 0;
  1330. fail_unlisten:
  1331. dlm_allow_conn = 0;
  1332. con = nodeid2con(0,0);
  1333. if (con) {
  1334. close_connection(con, false);
  1335. kmem_cache_free(con_cache, con);
  1336. }
  1337. fail_destroy:
  1338. kmem_cache_destroy(con_cache);
  1339. fail:
  1340. return error;
  1341. }