lowcomms.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  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 <linux/sctp.h>
  54. #include <net/sctp/sctp.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_INIT_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. new_con->sctp_assoc = sn->sn_assoc_change.sac_assoc_id;
  564. /* Send any pending writes */
  565. clear_bit(CF_CONNECT_PENDING, &new_con->flags);
  566. clear_bit(CF_INIT_PENDING, &new_con->flags);
  567. if (!test_and_set_bit(CF_WRITE_PENDING, &new_con->flags)) {
  568. queue_work(send_workqueue, &new_con->swork);
  569. }
  570. if (!test_and_set_bit(CF_READ_PENDING, &new_con->flags))
  571. queue_work(recv_workqueue, &new_con->rwork);
  572. }
  573. break;
  574. case SCTP_COMM_LOST:
  575. case SCTP_SHUTDOWN_COMP:
  576. {
  577. con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
  578. if (con) {
  579. con->sctp_assoc = 0;
  580. }
  581. }
  582. break;
  583. /* We don't know which INIT failed, so clear the PENDING flags
  584. * on them all. if assoc_id is zero then it will then try
  585. * again */
  586. case SCTP_CANT_STR_ASSOC:
  587. {
  588. log_print("Can't start SCTP association - retrying");
  589. sctp_init_failed();
  590. }
  591. break;
  592. default:
  593. log_print("unexpected SCTP assoc change id=%d state=%d",
  594. (int)sn->sn_assoc_change.sac_assoc_id,
  595. sn->sn_assoc_change.sac_state);
  596. }
  597. }
  598. }
  599. /* Data received from remote end */
  600. static int receive_from_sock(struct connection *con)
  601. {
  602. int ret = 0;
  603. struct msghdr msg = {};
  604. struct kvec iov[2];
  605. unsigned len;
  606. int r;
  607. int call_again_soon = 0;
  608. int nvec;
  609. char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
  610. mutex_lock(&con->sock_mutex);
  611. if (con->sock == NULL) {
  612. ret = -EAGAIN;
  613. goto out_close;
  614. }
  615. if (con->rx_page == NULL) {
  616. /*
  617. * This doesn't need to be atomic, but I think it should
  618. * improve performance if it is.
  619. */
  620. con->rx_page = alloc_page(GFP_ATOMIC);
  621. if (con->rx_page == NULL)
  622. goto out_resched;
  623. cbuf_init(&con->cb, PAGE_CACHE_SIZE);
  624. }
  625. /* Only SCTP needs these really */
  626. memset(&incmsg, 0, sizeof(incmsg));
  627. msg.msg_control = incmsg;
  628. msg.msg_controllen = sizeof(incmsg);
  629. /*
  630. * iov[0] is the bit of the circular buffer between the current end
  631. * point (cb.base + cb.len) and the end of the buffer.
  632. */
  633. iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
  634. iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
  635. iov[1].iov_len = 0;
  636. nvec = 1;
  637. /*
  638. * iov[1] is the bit of the circular buffer between the start of the
  639. * buffer and the start of the currently used section (cb.base)
  640. */
  641. if (cbuf_data(&con->cb) >= con->cb.base) {
  642. iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb);
  643. iov[1].iov_len = con->cb.base;
  644. iov[1].iov_base = page_address(con->rx_page);
  645. nvec = 2;
  646. }
  647. len = iov[0].iov_len + iov[1].iov_len;
  648. r = ret = kernel_recvmsg(con->sock, &msg, iov, nvec, len,
  649. MSG_DONTWAIT | MSG_NOSIGNAL);
  650. if (ret <= 0)
  651. goto out_close;
  652. /* Process SCTP notifications */
  653. if (msg.msg_flags & MSG_NOTIFICATION) {
  654. msg.msg_control = incmsg;
  655. msg.msg_controllen = sizeof(incmsg);
  656. process_sctp_notification(con, &msg,
  657. page_address(con->rx_page) + con->cb.base);
  658. mutex_unlock(&con->sock_mutex);
  659. return 0;
  660. }
  661. BUG_ON(con->nodeid == 0);
  662. if (ret == len)
  663. call_again_soon = 1;
  664. cbuf_add(&con->cb, ret);
  665. ret = dlm_process_incoming_buffer(con->nodeid,
  666. page_address(con->rx_page),
  667. con->cb.base, con->cb.len,
  668. PAGE_CACHE_SIZE);
  669. if (ret == -EBADMSG) {
  670. log_print("lowcomms: addr=%p, base=%u, len=%u, "
  671. "iov_len=%u, iov_base[0]=%p, read=%d",
  672. page_address(con->rx_page), con->cb.base, con->cb.len,
  673. len, iov[0].iov_base, r);
  674. }
  675. if (ret < 0)
  676. goto out_close;
  677. cbuf_eat(&con->cb, ret);
  678. if (cbuf_empty(&con->cb) && !call_again_soon) {
  679. __free_page(con->rx_page);
  680. con->rx_page = NULL;
  681. }
  682. if (call_again_soon)
  683. goto out_resched;
  684. mutex_unlock(&con->sock_mutex);
  685. return 0;
  686. out_resched:
  687. if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
  688. queue_work(recv_workqueue, &con->rwork);
  689. mutex_unlock(&con->sock_mutex);
  690. return -EAGAIN;
  691. out_close:
  692. mutex_unlock(&con->sock_mutex);
  693. if (ret != -EAGAIN) {
  694. close_connection(con, false);
  695. /* Reconnect when there is something to send */
  696. }
  697. /* Don't return success if we really got EOF */
  698. if (ret == 0)
  699. ret = -EAGAIN;
  700. return ret;
  701. }
  702. /* Listening socket is busy, accept a connection */
  703. static int tcp_accept_from_sock(struct connection *con)
  704. {
  705. int result;
  706. struct sockaddr_storage peeraddr;
  707. struct socket *newsock;
  708. int len;
  709. int nodeid;
  710. struct connection *newcon;
  711. struct connection *addcon;
  712. mutex_lock(&connections_lock);
  713. if (!dlm_allow_conn) {
  714. mutex_unlock(&connections_lock);
  715. return -1;
  716. }
  717. mutex_unlock(&connections_lock);
  718. memset(&peeraddr, 0, sizeof(peeraddr));
  719. result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
  720. IPPROTO_TCP, &newsock);
  721. if (result < 0)
  722. return -ENOMEM;
  723. mutex_lock_nested(&con->sock_mutex, 0);
  724. result = -ENOTCONN;
  725. if (con->sock == NULL)
  726. goto accept_err;
  727. newsock->type = con->sock->type;
  728. newsock->ops = con->sock->ops;
  729. result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK);
  730. if (result < 0)
  731. goto accept_err;
  732. /* Get the connected socket's peer */
  733. memset(&peeraddr, 0, sizeof(peeraddr));
  734. if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr,
  735. &len, 2)) {
  736. result = -ECONNABORTED;
  737. goto accept_err;
  738. }
  739. /* Get the new node's NODEID */
  740. make_sockaddr(&peeraddr, 0, &len);
  741. if (addr_to_nodeid(&peeraddr, &nodeid)) {
  742. unsigned char *b=(unsigned char *)&peeraddr;
  743. log_print("connect from non cluster node");
  744. print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
  745. b, sizeof(struct sockaddr_storage));
  746. sock_release(newsock);
  747. mutex_unlock(&con->sock_mutex);
  748. return -1;
  749. }
  750. log_print("got connection from %d", nodeid);
  751. /* Check to see if we already have a connection to this node. This
  752. * could happen if the two nodes initiate a connection at roughly
  753. * the same time and the connections cross on the wire.
  754. * In this case we store the incoming one in "othercon"
  755. */
  756. newcon = nodeid2con(nodeid, GFP_NOFS);
  757. if (!newcon) {
  758. result = -ENOMEM;
  759. goto accept_err;
  760. }
  761. mutex_lock_nested(&newcon->sock_mutex, 1);
  762. if (newcon->sock) {
  763. struct connection *othercon = newcon->othercon;
  764. if (!othercon) {
  765. othercon = kmem_cache_zalloc(con_cache, GFP_NOFS);
  766. if (!othercon) {
  767. log_print("failed to allocate incoming socket");
  768. mutex_unlock(&newcon->sock_mutex);
  769. result = -ENOMEM;
  770. goto accept_err;
  771. }
  772. othercon->nodeid = nodeid;
  773. othercon->rx_action = receive_from_sock;
  774. mutex_init(&othercon->sock_mutex);
  775. INIT_WORK(&othercon->swork, process_send_sockets);
  776. INIT_WORK(&othercon->rwork, process_recv_sockets);
  777. set_bit(CF_IS_OTHERCON, &othercon->flags);
  778. }
  779. if (!othercon->sock) {
  780. newcon->othercon = othercon;
  781. othercon->sock = newsock;
  782. newsock->sk->sk_user_data = othercon;
  783. add_sock(newsock, othercon);
  784. addcon = othercon;
  785. }
  786. else {
  787. printk("Extra connection from node %d attempted\n", nodeid);
  788. result = -EAGAIN;
  789. mutex_unlock(&newcon->sock_mutex);
  790. goto accept_err;
  791. }
  792. }
  793. else {
  794. newsock->sk->sk_user_data = newcon;
  795. newcon->rx_action = receive_from_sock;
  796. add_sock(newsock, newcon);
  797. addcon = newcon;
  798. }
  799. mutex_unlock(&newcon->sock_mutex);
  800. /*
  801. * Add it to the active queue in case we got data
  802. * between processing the accept adding the socket
  803. * to the read_sockets list
  804. */
  805. if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
  806. queue_work(recv_workqueue, &addcon->rwork);
  807. mutex_unlock(&con->sock_mutex);
  808. return 0;
  809. accept_err:
  810. mutex_unlock(&con->sock_mutex);
  811. sock_release(newsock);
  812. if (result != -EAGAIN)
  813. log_print("error accepting connection from node: %d", result);
  814. return result;
  815. }
  816. static void free_entry(struct writequeue_entry *e)
  817. {
  818. __free_page(e->page);
  819. kfree(e);
  820. }
  821. /* Initiate an SCTP association.
  822. This is a special case of send_to_sock() in that we don't yet have a
  823. peeled-off socket for this association, so we use the listening socket
  824. and add the primary IP address of the remote node.
  825. */
  826. static void sctp_init_assoc(struct connection *con)
  827. {
  828. struct sockaddr_storage rem_addr;
  829. char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
  830. struct msghdr outmessage;
  831. struct cmsghdr *cmsg;
  832. struct sctp_sndrcvinfo *sinfo;
  833. struct connection *base_con;
  834. struct writequeue_entry *e;
  835. int len, offset;
  836. int ret;
  837. int addrlen;
  838. struct kvec iov[1];
  839. if (test_and_set_bit(CF_INIT_PENDING, &con->flags))
  840. return;
  841. if (con->retries++ > MAX_CONNECT_RETRIES)
  842. return;
  843. if (nodeid_to_addr(con->nodeid, NULL, (struct sockaddr *)&rem_addr)) {
  844. log_print("no address for nodeid %d", con->nodeid);
  845. return;
  846. }
  847. base_con = nodeid2con(0, 0);
  848. BUG_ON(base_con == NULL);
  849. make_sockaddr(&rem_addr, dlm_config.ci_tcp_port, &addrlen);
  850. outmessage.msg_name = &rem_addr;
  851. outmessage.msg_namelen = addrlen;
  852. outmessage.msg_control = outcmsg;
  853. outmessage.msg_controllen = sizeof(outcmsg);
  854. outmessage.msg_flags = MSG_EOR;
  855. spin_lock(&con->writequeue_lock);
  856. if (list_empty(&con->writequeue)) {
  857. spin_unlock(&con->writequeue_lock);
  858. log_print("writequeue empty for nodeid %d", con->nodeid);
  859. return;
  860. }
  861. e = list_first_entry(&con->writequeue, struct writequeue_entry, list);
  862. len = e->len;
  863. offset = e->offset;
  864. spin_unlock(&con->writequeue_lock);
  865. /* Send the first block off the write queue */
  866. iov[0].iov_base = page_address(e->page)+offset;
  867. iov[0].iov_len = len;
  868. cmsg = CMSG_FIRSTHDR(&outmessage);
  869. cmsg->cmsg_level = IPPROTO_SCTP;
  870. cmsg->cmsg_type = SCTP_SNDRCV;
  871. cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
  872. sinfo = CMSG_DATA(cmsg);
  873. memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
  874. sinfo->sinfo_ppid = cpu_to_le32(dlm_our_nodeid());
  875. outmessage.msg_controllen = cmsg->cmsg_len;
  876. ret = kernel_sendmsg(base_con->sock, &outmessage, iov, 1, len);
  877. if (ret < 0) {
  878. log_print("Send first packet to node %d failed: %d",
  879. con->nodeid, ret);
  880. /* Try again later */
  881. clear_bit(CF_CONNECT_PENDING, &con->flags);
  882. clear_bit(CF_INIT_PENDING, &con->flags);
  883. }
  884. else {
  885. spin_lock(&con->writequeue_lock);
  886. e->offset += ret;
  887. e->len -= ret;
  888. if (e->len == 0 && e->users == 0) {
  889. list_del(&e->list);
  890. free_entry(e);
  891. }
  892. spin_unlock(&con->writequeue_lock);
  893. }
  894. }
  895. /* Connect a new socket to its peer */
  896. static void tcp_connect_to_sock(struct connection *con)
  897. {
  898. struct sockaddr_storage saddr, src_addr;
  899. int addr_len;
  900. struct socket *sock = NULL;
  901. int one = 1;
  902. int result;
  903. if (con->nodeid == 0) {
  904. log_print("attempt to connect sock 0 foiled");
  905. return;
  906. }
  907. mutex_lock(&con->sock_mutex);
  908. if (con->retries++ > MAX_CONNECT_RETRIES)
  909. goto out;
  910. /* Some odd races can cause double-connects, ignore them */
  911. if (con->sock)
  912. goto out;
  913. /* Create a socket to communicate with */
  914. result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
  915. IPPROTO_TCP, &sock);
  916. if (result < 0)
  917. goto out_err;
  918. memset(&saddr, 0, sizeof(saddr));
  919. result = nodeid_to_addr(con->nodeid, &saddr, NULL);
  920. if (result < 0) {
  921. log_print("no address for nodeid %d", con->nodeid);
  922. goto out_err;
  923. }
  924. sock->sk->sk_user_data = con;
  925. con->rx_action = receive_from_sock;
  926. con->connect_action = tcp_connect_to_sock;
  927. add_sock(sock, con);
  928. /* Bind to our cluster-known address connecting to avoid
  929. routing problems */
  930. memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
  931. make_sockaddr(&src_addr, 0, &addr_len);
  932. result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
  933. addr_len);
  934. if (result < 0) {
  935. log_print("could not bind for connect: %d", result);
  936. /* This *may* not indicate a critical error */
  937. }
  938. make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
  939. log_print("connecting to %d", con->nodeid);
  940. /* Turn off Nagle's algorithm */
  941. kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
  942. sizeof(one));
  943. result = sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
  944. O_NONBLOCK);
  945. if (result == -EINPROGRESS)
  946. result = 0;
  947. if (result == 0)
  948. goto out;
  949. out_err:
  950. if (con->sock) {
  951. sock_release(con->sock);
  952. con->sock = NULL;
  953. } else if (sock) {
  954. sock_release(sock);
  955. }
  956. /*
  957. * Some errors are fatal and this list might need adjusting. For other
  958. * errors we try again until the max number of retries is reached.
  959. */
  960. if (result != -EHOSTUNREACH &&
  961. result != -ENETUNREACH &&
  962. result != -ENETDOWN &&
  963. result != -EINVAL &&
  964. result != -EPROTONOSUPPORT) {
  965. log_print("connect %d try %d error %d", con->nodeid,
  966. con->retries, result);
  967. mutex_unlock(&con->sock_mutex);
  968. msleep(1000);
  969. lowcomms_connect_sock(con);
  970. return;
  971. }
  972. out:
  973. mutex_unlock(&con->sock_mutex);
  974. return;
  975. }
  976. static struct socket *tcp_create_listen_sock(struct connection *con,
  977. struct sockaddr_storage *saddr)
  978. {
  979. struct socket *sock = NULL;
  980. int result = 0;
  981. int one = 1;
  982. int addr_len;
  983. if (dlm_local_addr[0]->ss_family == AF_INET)
  984. addr_len = sizeof(struct sockaddr_in);
  985. else
  986. addr_len = sizeof(struct sockaddr_in6);
  987. /* Create a socket to communicate with */
  988. result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
  989. IPPROTO_TCP, &sock);
  990. if (result < 0) {
  991. log_print("Can't create listening comms socket");
  992. goto create_out;
  993. }
  994. /* Turn off Nagle's algorithm */
  995. kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
  996. sizeof(one));
  997. result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  998. (char *)&one, sizeof(one));
  999. if (result < 0) {
  1000. log_print("Failed to set SO_REUSEADDR on socket: %d", result);
  1001. }
  1002. con->rx_action = tcp_accept_from_sock;
  1003. con->connect_action = tcp_connect_to_sock;
  1004. /* Bind to our port */
  1005. make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
  1006. result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
  1007. if (result < 0) {
  1008. log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
  1009. sock_release(sock);
  1010. sock = NULL;
  1011. con->sock = NULL;
  1012. goto create_out;
  1013. }
  1014. result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
  1015. (char *)&one, sizeof(one));
  1016. if (result < 0) {
  1017. log_print("Set keepalive failed: %d", result);
  1018. }
  1019. result = sock->ops->listen(sock, 5);
  1020. if (result < 0) {
  1021. log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
  1022. sock_release(sock);
  1023. sock = NULL;
  1024. goto create_out;
  1025. }
  1026. create_out:
  1027. return sock;
  1028. }
  1029. /* Get local addresses */
  1030. static void init_local(void)
  1031. {
  1032. struct sockaddr_storage sas, *addr;
  1033. int i;
  1034. dlm_local_count = 0;
  1035. for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
  1036. if (dlm_our_addr(&sas, i))
  1037. break;
  1038. addr = kmalloc(sizeof(*addr), GFP_NOFS);
  1039. if (!addr)
  1040. break;
  1041. memcpy(addr, &sas, sizeof(*addr));
  1042. dlm_local_addr[dlm_local_count++] = addr;
  1043. }
  1044. }
  1045. /* Bind to an IP address. SCTP allows multiple address so it can do
  1046. multi-homing */
  1047. static int add_sctp_bind_addr(struct connection *sctp_con,
  1048. struct sockaddr_storage *addr,
  1049. int addr_len, int num)
  1050. {
  1051. int result = 0;
  1052. if (num == 1)
  1053. result = kernel_bind(sctp_con->sock,
  1054. (struct sockaddr *) addr,
  1055. addr_len);
  1056. else
  1057. result = kernel_setsockopt(sctp_con->sock, SOL_SCTP,
  1058. SCTP_SOCKOPT_BINDX_ADD,
  1059. (char *)addr, addr_len);
  1060. if (result < 0)
  1061. log_print("Can't bind to port %d addr number %d",
  1062. dlm_config.ci_tcp_port, num);
  1063. return result;
  1064. }
  1065. /* Initialise SCTP socket and bind to all interfaces */
  1066. static int sctp_listen_for_all(void)
  1067. {
  1068. struct socket *sock = NULL;
  1069. struct sockaddr_storage localaddr;
  1070. struct sctp_event_subscribe subscribe;
  1071. int result = -EINVAL, num = 1, i, addr_len;
  1072. struct connection *con = nodeid2con(0, GFP_NOFS);
  1073. int bufsize = NEEDED_RMEM;
  1074. if (!con)
  1075. return -ENOMEM;
  1076. log_print("Using SCTP for communications");
  1077. result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_SEQPACKET,
  1078. IPPROTO_SCTP, &sock);
  1079. if (result < 0) {
  1080. log_print("Can't create comms socket, check SCTP is loaded");
  1081. goto out;
  1082. }
  1083. /* Listen for events */
  1084. memset(&subscribe, 0, sizeof(subscribe));
  1085. subscribe.sctp_data_io_event = 1;
  1086. subscribe.sctp_association_event = 1;
  1087. subscribe.sctp_send_failure_event = 1;
  1088. subscribe.sctp_shutdown_event = 1;
  1089. subscribe.sctp_partial_delivery_event = 1;
  1090. result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUFFORCE,
  1091. (char *)&bufsize, sizeof(bufsize));
  1092. if (result)
  1093. log_print("Error increasing buffer space on socket %d", result);
  1094. result = kernel_setsockopt(sock, SOL_SCTP, SCTP_EVENTS,
  1095. (char *)&subscribe, sizeof(subscribe));
  1096. if (result < 0) {
  1097. log_print("Failed to set SCTP_EVENTS on socket: result=%d",
  1098. result);
  1099. goto create_delsock;
  1100. }
  1101. /* Init con struct */
  1102. sock->sk->sk_user_data = con;
  1103. con->sock = sock;
  1104. con->sock->sk->sk_data_ready = lowcomms_data_ready;
  1105. con->rx_action = receive_from_sock;
  1106. con->connect_action = sctp_init_assoc;
  1107. /* Bind to all interfaces. */
  1108. for (i = 0; i < dlm_local_count; i++) {
  1109. memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
  1110. make_sockaddr(&localaddr, dlm_config.ci_tcp_port, &addr_len);
  1111. result = add_sctp_bind_addr(con, &localaddr, addr_len, num);
  1112. if (result)
  1113. goto create_delsock;
  1114. ++num;
  1115. }
  1116. result = sock->ops->listen(sock, 5);
  1117. if (result < 0) {
  1118. log_print("Can't set socket listening");
  1119. goto create_delsock;
  1120. }
  1121. return 0;
  1122. create_delsock:
  1123. sock_release(sock);
  1124. con->sock = NULL;
  1125. out:
  1126. return result;
  1127. }
  1128. static int tcp_listen_for_all(void)
  1129. {
  1130. struct socket *sock = NULL;
  1131. struct connection *con = nodeid2con(0, GFP_NOFS);
  1132. int result = -EINVAL;
  1133. if (!con)
  1134. return -ENOMEM;
  1135. /* We don't support multi-homed hosts */
  1136. if (dlm_local_addr[1] != NULL) {
  1137. log_print("TCP protocol can't handle multi-homed hosts, "
  1138. "try SCTP");
  1139. return -EINVAL;
  1140. }
  1141. log_print("Using TCP for communications");
  1142. sock = tcp_create_listen_sock(con, dlm_local_addr[0]);
  1143. if (sock) {
  1144. add_sock(sock, con);
  1145. result = 0;
  1146. }
  1147. else {
  1148. result = -EADDRINUSE;
  1149. }
  1150. return result;
  1151. }
  1152. static struct writequeue_entry *new_writequeue_entry(struct connection *con,
  1153. gfp_t allocation)
  1154. {
  1155. struct writequeue_entry *entry;
  1156. entry = kmalloc(sizeof(struct writequeue_entry), allocation);
  1157. if (!entry)
  1158. return NULL;
  1159. entry->page = alloc_page(allocation);
  1160. if (!entry->page) {
  1161. kfree(entry);
  1162. return NULL;
  1163. }
  1164. entry->offset = 0;
  1165. entry->len = 0;
  1166. entry->end = 0;
  1167. entry->users = 0;
  1168. entry->con = con;
  1169. return entry;
  1170. }
  1171. void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
  1172. {
  1173. struct connection *con;
  1174. struct writequeue_entry *e;
  1175. int offset = 0;
  1176. con = nodeid2con(nodeid, allocation);
  1177. if (!con)
  1178. return NULL;
  1179. spin_lock(&con->writequeue_lock);
  1180. e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
  1181. if ((&e->list == &con->writequeue) ||
  1182. (PAGE_CACHE_SIZE - e->end < len)) {
  1183. e = NULL;
  1184. } else {
  1185. offset = e->end;
  1186. e->end += len;
  1187. e->users++;
  1188. }
  1189. spin_unlock(&con->writequeue_lock);
  1190. if (e) {
  1191. got_one:
  1192. *ppc = page_address(e->page) + offset;
  1193. return e;
  1194. }
  1195. e = new_writequeue_entry(con, allocation);
  1196. if (e) {
  1197. spin_lock(&con->writequeue_lock);
  1198. offset = e->end;
  1199. e->end += len;
  1200. e->users++;
  1201. list_add_tail(&e->list, &con->writequeue);
  1202. spin_unlock(&con->writequeue_lock);
  1203. goto got_one;
  1204. }
  1205. return NULL;
  1206. }
  1207. void dlm_lowcomms_commit_buffer(void *mh)
  1208. {
  1209. struct writequeue_entry *e = (struct writequeue_entry *)mh;
  1210. struct connection *con = e->con;
  1211. int users;
  1212. spin_lock(&con->writequeue_lock);
  1213. users = --e->users;
  1214. if (users)
  1215. goto out;
  1216. e->len = e->end - e->offset;
  1217. spin_unlock(&con->writequeue_lock);
  1218. if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
  1219. queue_work(send_workqueue, &con->swork);
  1220. }
  1221. return;
  1222. out:
  1223. spin_unlock(&con->writequeue_lock);
  1224. return;
  1225. }
  1226. /* Send a message */
  1227. static void send_to_sock(struct connection *con)
  1228. {
  1229. int ret = 0;
  1230. const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
  1231. struct writequeue_entry *e;
  1232. int len, offset;
  1233. int count = 0;
  1234. mutex_lock(&con->sock_mutex);
  1235. if (con->sock == NULL)
  1236. goto out_connect;
  1237. spin_lock(&con->writequeue_lock);
  1238. for (;;) {
  1239. e = list_entry(con->writequeue.next, struct writequeue_entry,
  1240. list);
  1241. if ((struct list_head *) e == &con->writequeue)
  1242. break;
  1243. len = e->len;
  1244. offset = e->offset;
  1245. BUG_ON(len == 0 && e->users == 0);
  1246. spin_unlock(&con->writequeue_lock);
  1247. ret = 0;
  1248. if (len) {
  1249. ret = kernel_sendpage(con->sock, e->page, offset, len,
  1250. msg_flags);
  1251. if (ret == -EAGAIN || ret == 0) {
  1252. if (ret == -EAGAIN &&
  1253. test_bit(SOCK_ASYNC_NOSPACE, &con->sock->flags) &&
  1254. !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
  1255. /* Notify TCP that we're limited by the
  1256. * application window size.
  1257. */
  1258. set_bit(SOCK_NOSPACE, &con->sock->flags);
  1259. con->sock->sk->sk_write_pending++;
  1260. }
  1261. cond_resched();
  1262. goto out;
  1263. } else if (ret < 0)
  1264. goto send_error;
  1265. }
  1266. /* Don't starve people filling buffers */
  1267. if (++count >= MAX_SEND_MSG_COUNT) {
  1268. cond_resched();
  1269. count = 0;
  1270. }
  1271. spin_lock(&con->writequeue_lock);
  1272. e->offset += ret;
  1273. e->len -= ret;
  1274. if (e->len == 0 && e->users == 0) {
  1275. list_del(&e->list);
  1276. free_entry(e);
  1277. }
  1278. }
  1279. spin_unlock(&con->writequeue_lock);
  1280. out:
  1281. mutex_unlock(&con->sock_mutex);
  1282. return;
  1283. send_error:
  1284. mutex_unlock(&con->sock_mutex);
  1285. close_connection(con, false);
  1286. lowcomms_connect_sock(con);
  1287. return;
  1288. out_connect:
  1289. mutex_unlock(&con->sock_mutex);
  1290. if (!test_bit(CF_INIT_PENDING, &con->flags))
  1291. lowcomms_connect_sock(con);
  1292. }
  1293. static void clean_one_writequeue(struct connection *con)
  1294. {
  1295. struct writequeue_entry *e, *safe;
  1296. spin_lock(&con->writequeue_lock);
  1297. list_for_each_entry_safe(e, safe, &con->writequeue, list) {
  1298. list_del(&e->list);
  1299. free_entry(e);
  1300. }
  1301. spin_unlock(&con->writequeue_lock);
  1302. }
  1303. /* Called from recovery when it knows that a node has
  1304. left the cluster */
  1305. int dlm_lowcomms_close(int nodeid)
  1306. {
  1307. struct connection *con;
  1308. struct dlm_node_addr *na;
  1309. log_print("closing connection to node %d", nodeid);
  1310. con = nodeid2con(nodeid, 0);
  1311. if (con) {
  1312. clear_bit(CF_CONNECT_PENDING, &con->flags);
  1313. clear_bit(CF_WRITE_PENDING, &con->flags);
  1314. set_bit(CF_CLOSE, &con->flags);
  1315. if (cancel_work_sync(&con->swork))
  1316. log_print("canceled swork for node %d", nodeid);
  1317. if (cancel_work_sync(&con->rwork))
  1318. log_print("canceled rwork for node %d", nodeid);
  1319. clean_one_writequeue(con);
  1320. close_connection(con, true);
  1321. }
  1322. spin_lock(&dlm_node_addrs_spin);
  1323. na = find_node_addr(nodeid);
  1324. if (na) {
  1325. list_del(&na->list);
  1326. while (na->addr_count--)
  1327. kfree(na->addr[na->addr_count]);
  1328. kfree(na);
  1329. }
  1330. spin_unlock(&dlm_node_addrs_spin);
  1331. return 0;
  1332. }
  1333. /* Receive workqueue function */
  1334. static void process_recv_sockets(struct work_struct *work)
  1335. {
  1336. struct connection *con = container_of(work, struct connection, rwork);
  1337. int err;
  1338. clear_bit(CF_READ_PENDING, &con->flags);
  1339. do {
  1340. err = con->rx_action(con);
  1341. } while (!err);
  1342. }
  1343. /* Send workqueue function */
  1344. static void process_send_sockets(struct work_struct *work)
  1345. {
  1346. struct connection *con = container_of(work, struct connection, swork);
  1347. if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
  1348. con->connect_action(con);
  1349. set_bit(CF_WRITE_PENDING, &con->flags);
  1350. }
  1351. if (test_and_clear_bit(CF_WRITE_PENDING, &con->flags))
  1352. send_to_sock(con);
  1353. }
  1354. /* Discard all entries on the write queues */
  1355. static void clean_writequeues(void)
  1356. {
  1357. foreach_conn(clean_one_writequeue);
  1358. }
  1359. static void work_stop(void)
  1360. {
  1361. destroy_workqueue(recv_workqueue);
  1362. destroy_workqueue(send_workqueue);
  1363. }
  1364. static int work_start(void)
  1365. {
  1366. recv_workqueue = alloc_workqueue("dlm_recv",
  1367. WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
  1368. if (!recv_workqueue) {
  1369. log_print("can't start dlm_recv");
  1370. return -ENOMEM;
  1371. }
  1372. send_workqueue = alloc_workqueue("dlm_send",
  1373. WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
  1374. if (!send_workqueue) {
  1375. log_print("can't start dlm_send");
  1376. destroy_workqueue(recv_workqueue);
  1377. return -ENOMEM;
  1378. }
  1379. return 0;
  1380. }
  1381. static void stop_conn(struct connection *con)
  1382. {
  1383. con->flags |= 0x0F;
  1384. if (con->sock && con->sock->sk)
  1385. con->sock->sk->sk_user_data = NULL;
  1386. }
  1387. static void free_conn(struct connection *con)
  1388. {
  1389. close_connection(con, true);
  1390. if (con->othercon)
  1391. kmem_cache_free(con_cache, con->othercon);
  1392. hlist_del(&con->list);
  1393. kmem_cache_free(con_cache, con);
  1394. }
  1395. void dlm_lowcomms_stop(void)
  1396. {
  1397. /* Set all the flags to prevent any
  1398. socket activity.
  1399. */
  1400. mutex_lock(&connections_lock);
  1401. dlm_allow_conn = 0;
  1402. foreach_conn(stop_conn);
  1403. mutex_unlock(&connections_lock);
  1404. work_stop();
  1405. mutex_lock(&connections_lock);
  1406. clean_writequeues();
  1407. foreach_conn(free_conn);
  1408. mutex_unlock(&connections_lock);
  1409. kmem_cache_destroy(con_cache);
  1410. }
  1411. int dlm_lowcomms_start(void)
  1412. {
  1413. int error = -EINVAL;
  1414. struct connection *con;
  1415. int i;
  1416. for (i = 0; i < CONN_HASH_SIZE; i++)
  1417. INIT_HLIST_HEAD(&connection_hash[i]);
  1418. init_local();
  1419. if (!dlm_local_count) {
  1420. error = -ENOTCONN;
  1421. log_print("no local IP address has been set");
  1422. goto fail;
  1423. }
  1424. error = -ENOMEM;
  1425. con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
  1426. __alignof__(struct connection), 0,
  1427. NULL);
  1428. if (!con_cache)
  1429. goto fail;
  1430. error = work_start();
  1431. if (error)
  1432. goto fail_destroy;
  1433. dlm_allow_conn = 1;
  1434. /* Start listening */
  1435. if (dlm_config.ci_protocol == 0)
  1436. error = tcp_listen_for_all();
  1437. else
  1438. error = sctp_listen_for_all();
  1439. if (error)
  1440. goto fail_unlisten;
  1441. return 0;
  1442. fail_unlisten:
  1443. dlm_allow_conn = 0;
  1444. con = nodeid2con(0,0);
  1445. if (con) {
  1446. close_connection(con, false);
  1447. kmem_cache_free(con_cache, con);
  1448. }
  1449. fail_destroy:
  1450. kmem_cache_destroy(con_cache);
  1451. fail:
  1452. return error;
  1453. }
  1454. void dlm_lowcomms_exit(void)
  1455. {
  1456. struct dlm_node_addr *na, *safe;
  1457. spin_lock(&dlm_node_addrs_spin);
  1458. list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) {
  1459. list_del(&na->list);
  1460. while (na->addr_count--)
  1461. kfree(na->addr[na->addr_count]);
  1462. kfree(na);
  1463. }
  1464. spin_unlock(&dlm_node_addrs_spin);
  1465. }