lowcomms.c 45 KB

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