lowcomms.c 42 KB

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