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