lowcomms.c 42 KB

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