lowcomms.c 42 KB

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