lowcomms.c 44 KB

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