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