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