lowcomms.c 44 KB

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