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