lowcomms.c 40 KB

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