Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * IPVS An implementation of the IP virtual server support for the |
| 3 | * LINUX operating system. IPVS is now implemented as a module |
| 4 | * over the NetFilter framework. IPVS can be used to build a |
| 5 | * high-performance and highly available server based on a |
| 6 | * cluster of servers. |
| 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Authors: Wensong Zhang <wensong@linuxvirtualserver.org> |
| 9 | * |
| 10 | * ip_vs_sync: sync connection info from master load balancer to backups |
| 11 | * through multicast |
| 12 | * |
| 13 | * Changes: |
| 14 | * Alexandre Cassen : Added master & backup support at a time. |
| 15 | * Alexandre Cassen : Added SyncID support for incoming sync |
| 16 | * messages filtering. |
| 17 | * Justin Ossevoort : Fix endian problem on sync message size. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/slab.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 22 | #include <linux/inetdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/net.h> |
| 24 | #include <linux/completion.h> |
| 25 | #include <linux/delay.h> |
| 26 | #include <linux/skbuff.h> |
| 27 | #include <linux/in.h> |
| 28 | #include <linux/igmp.h> /* for ip_mc_join_group */ |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 29 | #include <linux/udp.h> |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 30 | #include <linux/err.h> |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 31 | #include <linux/kthread.h> |
Sven Wegener | ba6fd85 | 2008-07-16 11:13:56 +0000 | [diff] [blame] | 32 | #include <linux/wait.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | #include <net/ip.h> |
| 35 | #include <net/sock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | #include <net/ip_vs.h> |
| 38 | |
| 39 | #define IP_VS_SYNC_GROUP 0xe0000051 /* multicast addr - 224.0.0.81 */ |
| 40 | #define IP_VS_SYNC_PORT 8848 /* multicast port */ |
| 41 | |
| 42 | |
| 43 | /* |
| 44 | * IPVS sync connection entry |
| 45 | */ |
| 46 | struct ip_vs_sync_conn { |
| 47 | __u8 reserved; |
| 48 | |
| 49 | /* Protocol, addresses and port numbers */ |
| 50 | __u8 protocol; /* Which protocol (TCP/UDP) */ |
Al Viro | 014d730 | 2006-09-28 14:29:52 -0700 | [diff] [blame] | 51 | __be16 cport; |
| 52 | __be16 vport; |
| 53 | __be16 dport; |
| 54 | __be32 caddr; /* client address */ |
| 55 | __be32 vaddr; /* virtual address */ |
| 56 | __be32 daddr; /* destination address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | /* Flags and state transition */ |
Al Viro | 014d730 | 2006-09-28 14:29:52 -0700 | [diff] [blame] | 59 | __be16 flags; /* status flags */ |
| 60 | __be16 state; /* state info */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | /* The sequence options start here */ |
| 63 | }; |
| 64 | |
| 65 | struct ip_vs_sync_conn_options { |
| 66 | struct ip_vs_seq in_seq; /* incoming seq. struct */ |
| 67 | struct ip_vs_seq out_seq; /* outgoing seq. struct */ |
| 68 | }; |
| 69 | |
Neil Horman | cc0191a | 2007-06-18 22:33:20 -0700 | [diff] [blame] | 70 | struct ip_vs_sync_thread_data { |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 71 | struct socket *sock; |
| 72 | char *buf; |
Neil Horman | cc0191a | 2007-06-18 22:33:20 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | #define SIMPLE_CONN_SIZE (sizeof(struct ip_vs_sync_conn)) |
| 76 | #define FULL_CONN_SIZE \ |
| 77 | (sizeof(struct ip_vs_sync_conn) + sizeof(struct ip_vs_sync_conn_options)) |
| 78 | |
| 79 | |
| 80 | /* |
| 81 | The master mulitcasts messages to the backup load balancers in the |
| 82 | following format. |
| 83 | |
| 84 | 0 1 2 3 |
| 85 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 86 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 87 | | Count Conns | SyncID | Size | |
| 88 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 89 | | | |
| 90 | | IPVS Sync Connection (1) | |
| 91 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 92 | | . | |
| 93 | | . | |
| 94 | | . | |
| 95 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 96 | | | |
| 97 | | IPVS Sync Connection (n) | |
| 98 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 99 | */ |
| 100 | |
| 101 | #define SYNC_MESG_HEADER_LEN 4 |
| 102 | |
| 103 | struct ip_vs_sync_mesg { |
| 104 | __u8 nr_conns; |
| 105 | __u8 syncid; |
| 106 | __u16 size; |
| 107 | |
| 108 | /* ip_vs_sync_conn entries start here */ |
| 109 | }; |
| 110 | |
| 111 | /* the maximum length of sync (sending/receiving) message */ |
| 112 | static int sync_send_mesg_maxlen; |
| 113 | static int sync_recv_mesg_maxlen; |
| 114 | |
| 115 | struct ip_vs_sync_buff { |
| 116 | struct list_head list; |
| 117 | unsigned long firstuse; |
| 118 | |
| 119 | /* pointers for the message data */ |
| 120 | struct ip_vs_sync_mesg *mesg; |
| 121 | unsigned char *head; |
| 122 | unsigned char *end; |
| 123 | }; |
| 124 | |
| 125 | |
| 126 | /* the sync_buff list head and the lock */ |
| 127 | static LIST_HEAD(ip_vs_sync_queue); |
| 128 | static DEFINE_SPINLOCK(ip_vs_sync_lock); |
| 129 | |
| 130 | /* current sync_buff for accepting new conn entries */ |
| 131 | static struct ip_vs_sync_buff *curr_sb = NULL; |
| 132 | static DEFINE_SPINLOCK(curr_sb_lock); |
| 133 | |
| 134 | /* ipvs sync daemon state */ |
| 135 | volatile int ip_vs_sync_state = IP_VS_STATE_NONE; |
| 136 | volatile int ip_vs_master_syncid = 0; |
| 137 | volatile int ip_vs_backup_syncid = 0; |
| 138 | |
| 139 | /* multicast interface name */ |
| 140 | char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN]; |
| 141 | char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN]; |
| 142 | |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 143 | /* sync daemon tasks */ |
| 144 | static struct task_struct *sync_master_thread; |
| 145 | static struct task_struct *sync_backup_thread; |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | /* multicast addr */ |
Sven Wegener | d564005 | 2008-07-16 11:13:35 +0000 | [diff] [blame] | 148 | static struct sockaddr_in mcast_addr = { |
| 149 | .sin_family = AF_INET, |
| 150 | .sin_port = __constant_htons(IP_VS_SYNC_PORT), |
| 151 | .sin_addr.s_addr = __constant_htonl(IP_VS_SYNC_GROUP), |
| 152 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 155 | static inline struct ip_vs_sync_buff *sb_dequeue(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
| 157 | struct ip_vs_sync_buff *sb; |
| 158 | |
| 159 | spin_lock_bh(&ip_vs_sync_lock); |
| 160 | if (list_empty(&ip_vs_sync_queue)) { |
| 161 | sb = NULL; |
| 162 | } else { |
| 163 | sb = list_entry(ip_vs_sync_queue.next, |
| 164 | struct ip_vs_sync_buff, |
| 165 | list); |
| 166 | list_del(&sb->list); |
| 167 | } |
| 168 | spin_unlock_bh(&ip_vs_sync_lock); |
| 169 | |
| 170 | return sb; |
| 171 | } |
| 172 | |
| 173 | static inline struct ip_vs_sync_buff * ip_vs_sync_buff_create(void) |
| 174 | { |
| 175 | struct ip_vs_sync_buff *sb; |
| 176 | |
| 177 | if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC))) |
| 178 | return NULL; |
| 179 | |
| 180 | if (!(sb->mesg=kmalloc(sync_send_mesg_maxlen, GFP_ATOMIC))) { |
| 181 | kfree(sb); |
| 182 | return NULL; |
| 183 | } |
| 184 | sb->mesg->nr_conns = 0; |
| 185 | sb->mesg->syncid = ip_vs_master_syncid; |
| 186 | sb->mesg->size = 4; |
| 187 | sb->head = (unsigned char *)sb->mesg + 4; |
| 188 | sb->end = (unsigned char *)sb->mesg + sync_send_mesg_maxlen; |
| 189 | sb->firstuse = jiffies; |
| 190 | return sb; |
| 191 | } |
| 192 | |
| 193 | static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff *sb) |
| 194 | { |
| 195 | kfree(sb->mesg); |
| 196 | kfree(sb); |
| 197 | } |
| 198 | |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 199 | static inline void sb_queue_tail(struct ip_vs_sync_buff *sb) |
| 200 | { |
| 201 | spin_lock(&ip_vs_sync_lock); |
| 202 | if (ip_vs_sync_state & IP_VS_STATE_MASTER) |
| 203 | list_add_tail(&sb->list, &ip_vs_sync_queue); |
| 204 | else |
| 205 | ip_vs_sync_buff_release(sb); |
| 206 | spin_unlock(&ip_vs_sync_lock); |
| 207 | } |
| 208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | /* |
| 210 | * Get the current sync buffer if it has been created for more |
| 211 | * than the specified time or the specified time is zero. |
| 212 | */ |
| 213 | static inline struct ip_vs_sync_buff * |
| 214 | get_curr_sync_buff(unsigned long time) |
| 215 | { |
| 216 | struct ip_vs_sync_buff *sb; |
| 217 | |
| 218 | spin_lock_bh(&curr_sb_lock); |
| 219 | if (curr_sb && (time == 0 || |
| 220 | time_before(jiffies - curr_sb->firstuse, time))) { |
| 221 | sb = curr_sb; |
| 222 | curr_sb = NULL; |
| 223 | } else |
| 224 | sb = NULL; |
| 225 | spin_unlock_bh(&curr_sb_lock); |
| 226 | return sb; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | /* |
| 231 | * Add an ip_vs_conn information into the current sync_buff. |
| 232 | * Called by ip_vs_in. |
| 233 | */ |
| 234 | void ip_vs_sync_conn(struct ip_vs_conn *cp) |
| 235 | { |
| 236 | struct ip_vs_sync_mesg *m; |
| 237 | struct ip_vs_sync_conn *s; |
| 238 | int len; |
| 239 | |
| 240 | spin_lock(&curr_sb_lock); |
| 241 | if (!curr_sb) { |
| 242 | if (!(curr_sb=ip_vs_sync_buff_create())) { |
| 243 | spin_unlock(&curr_sb_lock); |
| 244 | IP_VS_ERR("ip_vs_sync_buff_create failed.\n"); |
| 245 | return; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | len = (cp->flags & IP_VS_CONN_F_SEQ_MASK) ? FULL_CONN_SIZE : |
| 250 | SIMPLE_CONN_SIZE; |
| 251 | m = curr_sb->mesg; |
| 252 | s = (struct ip_vs_sync_conn *)curr_sb->head; |
| 253 | |
| 254 | /* copy members */ |
| 255 | s->protocol = cp->protocol; |
| 256 | s->cport = cp->cport; |
| 257 | s->vport = cp->vport; |
| 258 | s->dport = cp->dport; |
Julius Volz | e7ade46 | 2008-09-02 15:55:33 +0200 | [diff] [blame] | 259 | s->caddr = cp->caddr.ip; |
| 260 | s->vaddr = cp->vaddr.ip; |
| 261 | s->daddr = cp->daddr.ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | s->flags = htons(cp->flags & ~IP_VS_CONN_F_HASHED); |
| 263 | s->state = htons(cp->state); |
| 264 | if (cp->flags & IP_VS_CONN_F_SEQ_MASK) { |
| 265 | struct ip_vs_sync_conn_options *opt = |
| 266 | (struct ip_vs_sync_conn_options *)&s[1]; |
| 267 | memcpy(opt, &cp->in_seq, sizeof(*opt)); |
| 268 | } |
| 269 | |
| 270 | m->nr_conns++; |
| 271 | m->size += len; |
| 272 | curr_sb->head += len; |
| 273 | |
| 274 | /* check if there is a space for next one */ |
| 275 | if (curr_sb->head+FULL_CONN_SIZE > curr_sb->end) { |
| 276 | sb_queue_tail(curr_sb); |
| 277 | curr_sb = NULL; |
| 278 | } |
| 279 | spin_unlock(&curr_sb_lock); |
| 280 | |
| 281 | /* synchronize its controller if it has */ |
| 282 | if (cp->control) |
| 283 | ip_vs_sync_conn(cp->control); |
| 284 | } |
| 285 | |
| 286 | |
| 287 | /* |
| 288 | * Process received multicast message and create the corresponding |
| 289 | * ip_vs_conn entries. |
| 290 | */ |
| 291 | static void ip_vs_process_message(const char *buffer, const size_t buflen) |
| 292 | { |
| 293 | struct ip_vs_sync_mesg *m = (struct ip_vs_sync_mesg *)buffer; |
| 294 | struct ip_vs_sync_conn *s; |
| 295 | struct ip_vs_sync_conn_options *opt; |
| 296 | struct ip_vs_conn *cp; |
Andy Gospodarek | 5c81833 | 2007-10-29 04:35:45 -0700 | [diff] [blame] | 297 | struct ip_vs_protocol *pp; |
Rumen G. Bogdanovski | 1e356f9 | 2007-11-07 02:35:54 -0800 | [diff] [blame] | 298 | struct ip_vs_dest *dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | char *p; |
| 300 | int i; |
| 301 | |
Julian Anastasov | 2ad17de | 2008-04-29 03:21:23 -0700 | [diff] [blame] | 302 | if (buflen < sizeof(struct ip_vs_sync_mesg)) { |
| 303 | IP_VS_ERR_RL("sync message header too short\n"); |
| 304 | return; |
| 305 | } |
| 306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | /* Convert size back to host byte order */ |
| 308 | m->size = ntohs(m->size); |
| 309 | |
| 310 | if (buflen != m->size) { |
Julian Anastasov | 2ad17de | 2008-04-29 03:21:23 -0700 | [diff] [blame] | 311 | IP_VS_ERR_RL("bogus sync message size\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | return; |
| 313 | } |
| 314 | |
| 315 | /* SyncID sanity check */ |
| 316 | if (ip_vs_backup_syncid != 0 && m->syncid != ip_vs_backup_syncid) { |
| 317 | IP_VS_DBG(7, "Ignoring incoming msg with syncid = %d\n", |
| 318 | m->syncid); |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | p = (char *)buffer + sizeof(struct ip_vs_sync_mesg); |
| 323 | for (i=0; i<m->nr_conns; i++) { |
Rumen G. Bogdanovski | b209639 | 2007-11-19 21:53:27 -0800 | [diff] [blame] | 324 | unsigned flags, state; |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 325 | |
Julian Anastasov | 2ad17de | 2008-04-29 03:21:23 -0700 | [diff] [blame] | 326 | if (p + SIMPLE_CONN_SIZE > buffer+buflen) { |
| 327 | IP_VS_ERR_RL("bogus conn in sync message\n"); |
| 328 | return; |
| 329 | } |
| 330 | s = (struct ip_vs_sync_conn *) p; |
Rumen G. Bogdanovski | 7a4fbb1 | 2007-11-19 21:52:42 -0800 | [diff] [blame] | 331 | flags = ntohs(s->flags) | IP_VS_CONN_F_SYNC; |
Julian Anastasov | 2ad17de | 2008-04-29 03:21:23 -0700 | [diff] [blame] | 332 | flags &= ~IP_VS_CONN_F_HASHED; |
| 333 | if (flags & IP_VS_CONN_F_SEQ_MASK) { |
| 334 | opt = (struct ip_vs_sync_conn_options *)&s[1]; |
| 335 | p += FULL_CONN_SIZE; |
| 336 | if (p > buffer+buflen) { |
| 337 | IP_VS_ERR_RL("bogus conn options in sync message\n"); |
| 338 | return; |
| 339 | } |
| 340 | } else { |
| 341 | opt = NULL; |
| 342 | p += SIMPLE_CONN_SIZE; |
| 343 | } |
| 344 | |
Rumen G. Bogdanovski | b209639 | 2007-11-19 21:53:27 -0800 | [diff] [blame] | 345 | state = ntohs(s->state); |
Julian Anastasov | 2ad17de | 2008-04-29 03:21:23 -0700 | [diff] [blame] | 346 | if (!(flags & IP_VS_CONN_F_TEMPLATE)) { |
| 347 | pp = ip_vs_proto_get(s->protocol); |
| 348 | if (!pp) { |
| 349 | IP_VS_ERR_RL("Unsupported protocol %u in sync msg\n", |
| 350 | s->protocol); |
| 351 | continue; |
| 352 | } |
| 353 | if (state >= pp->num_states) { |
| 354 | IP_VS_DBG(2, "Invalid %s state %u in sync msg\n", |
| 355 | pp->name, state); |
| 356 | continue; |
| 357 | } |
| 358 | } else { |
| 359 | /* protocol in templates is not used for state/timeout */ |
| 360 | pp = NULL; |
| 361 | if (state > 0) { |
| 362 | IP_VS_DBG(2, "Invalid template state %u in sync msg\n", |
| 363 | state); |
| 364 | state = 0; |
| 365 | } |
| 366 | } |
| 367 | |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 368 | if (!(flags & IP_VS_CONN_F_TEMPLATE)) |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 369 | cp = ip_vs_conn_in_get(AF_INET, s->protocol, |
| 370 | (union nf_inet_addr *)&s->caddr, |
| 371 | s->cport, |
| 372 | (union nf_inet_addr *)&s->vaddr, |
| 373 | s->vport); |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 374 | else |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 375 | cp = ip_vs_ct_in_get(AF_INET, s->protocol, |
| 376 | (union nf_inet_addr *)&s->caddr, |
| 377 | s->cport, |
| 378 | (union nf_inet_addr *)&s->vaddr, |
| 379 | s->vport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | if (!cp) { |
Rumen G. Bogdanovski | 1e356f9 | 2007-11-07 02:35:54 -0800 | [diff] [blame] | 381 | /* |
| 382 | * Find the appropriate destination for the connection. |
| 383 | * If it is not found the connection will remain unbound |
| 384 | * but still handled. |
| 385 | */ |
Julius Volz | 7937df1 | 2008-09-02 15:55:48 +0200 | [diff] [blame^] | 386 | dest = ip_vs_find_dest(AF_INET, |
| 387 | (union nf_inet_addr *)&s->daddr, |
| 388 | s->dport, |
| 389 | (union nf_inet_addr *)&s->vaddr, |
| 390 | s->vport, |
Rumen G. Bogdanovski | 1e356f9 | 2007-11-07 02:35:54 -0800 | [diff] [blame] | 391 | s->protocol); |
Rumen G. Bogdanovski | b209639 | 2007-11-19 21:53:27 -0800 | [diff] [blame] | 392 | /* Set the approprite ativity flag */ |
| 393 | if (s->protocol == IPPROTO_TCP) { |
| 394 | if (state != IP_VS_TCP_S_ESTABLISHED) |
| 395 | flags |= IP_VS_CONN_F_INACTIVE; |
| 396 | else |
| 397 | flags &= ~IP_VS_CONN_F_INACTIVE; |
| 398 | } |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 399 | cp = ip_vs_conn_new(AF_INET, s->protocol, |
| 400 | (union nf_inet_addr *)s->caddr, |
| 401 | s->cport, |
| 402 | (union nf_inet_addr *)s->vaddr, |
| 403 | s->vport, |
| 404 | (union nf_inet_addr *)s->daddr, |
| 405 | s->dport, |
Rumen G. Bogdanovski | 1e356f9 | 2007-11-07 02:35:54 -0800 | [diff] [blame] | 406 | flags, dest); |
| 407 | if (dest) |
| 408 | atomic_dec(&dest->refcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | if (!cp) { |
| 410 | IP_VS_ERR("ip_vs_conn_new failed\n"); |
| 411 | return; |
| 412 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } else if (!cp->dest) { |
Rumen G. Bogdanovski | 1e356f9 | 2007-11-07 02:35:54 -0800 | [diff] [blame] | 414 | dest = ip_vs_try_bind_dest(cp); |
Julian Anastasov | 2ad17de | 2008-04-29 03:21:23 -0700 | [diff] [blame] | 415 | if (dest) |
Rumen G. Bogdanovski | 1e356f9 | 2007-11-07 02:35:54 -0800 | [diff] [blame] | 416 | atomic_dec(&dest->refcnt); |
Rumen G. Bogdanovski | b209639 | 2007-11-19 21:53:27 -0800 | [diff] [blame] | 417 | } else if ((cp->dest) && (cp->protocol == IPPROTO_TCP) && |
| 418 | (cp->state != state)) { |
| 419 | /* update active/inactive flag for the connection */ |
| 420 | dest = cp->dest; |
| 421 | if (!(cp->flags & IP_VS_CONN_F_INACTIVE) && |
| 422 | (state != IP_VS_TCP_S_ESTABLISHED)) { |
| 423 | atomic_dec(&dest->activeconns); |
| 424 | atomic_inc(&dest->inactconns); |
| 425 | cp->flags |= IP_VS_CONN_F_INACTIVE; |
| 426 | } else if ((cp->flags & IP_VS_CONN_F_INACTIVE) && |
| 427 | (state == IP_VS_TCP_S_ESTABLISHED)) { |
| 428 | atomic_inc(&dest->activeconns); |
| 429 | atomic_dec(&dest->inactconns); |
| 430 | cp->flags &= ~IP_VS_CONN_F_INACTIVE; |
| 431 | } |
| 432 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
Julian Anastasov | 2ad17de | 2008-04-29 03:21:23 -0700 | [diff] [blame] | 434 | if (opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | memcpy(&cp->in_seq, opt, sizeof(*opt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | atomic_set(&cp->in_pkts, sysctl_ip_vs_sync_threshold[0]); |
Rumen G. Bogdanovski | b209639 | 2007-11-19 21:53:27 -0800 | [diff] [blame] | 437 | cp->state = state; |
Julian Anastasov | 2ad17de | 2008-04-29 03:21:23 -0700 | [diff] [blame] | 438 | cp->old_state = cp->state; |
| 439 | /* |
| 440 | * We can not recover the right timeout for templates |
| 441 | * in all cases, we can not find the right fwmark |
| 442 | * virtual service. If needed, we can do it for |
| 443 | * non-fwmark persistent services. |
| 444 | */ |
| 445 | if (!(flags & IP_VS_CONN_F_TEMPLATE) && pp->timeout_table) |
| 446 | cp->timeout = pp->timeout_table[state]; |
| 447 | else |
| 448 | cp->timeout = (3*60*HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | ip_vs_conn_put(cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
| 453 | |
| 454 | /* |
| 455 | * Setup loopback of outgoing multicasts on a sending socket |
| 456 | */ |
| 457 | static void set_mcast_loop(struct sock *sk, u_char loop) |
| 458 | { |
| 459 | struct inet_sock *inet = inet_sk(sk); |
| 460 | |
| 461 | /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */ |
| 462 | lock_sock(sk); |
| 463 | inet->mc_loop = loop ? 1 : 0; |
| 464 | release_sock(sk); |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * Specify TTL for outgoing multicasts on a sending socket |
| 469 | */ |
| 470 | static void set_mcast_ttl(struct sock *sk, u_char ttl) |
| 471 | { |
| 472 | struct inet_sock *inet = inet_sk(sk); |
| 473 | |
| 474 | /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */ |
| 475 | lock_sock(sk); |
| 476 | inet->mc_ttl = ttl; |
| 477 | release_sock(sk); |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * Specifiy default interface for outgoing multicasts |
| 482 | */ |
| 483 | static int set_mcast_if(struct sock *sk, char *ifname) |
| 484 | { |
| 485 | struct net_device *dev; |
| 486 | struct inet_sock *inet = inet_sk(sk); |
| 487 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 488 | if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | return -ENODEV; |
| 490 | |
| 491 | if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if) |
| 492 | return -EINVAL; |
| 493 | |
| 494 | lock_sock(sk); |
| 495 | inet->mc_index = dev->ifindex; |
| 496 | /* inet->mc_addr = 0; */ |
| 497 | release_sock(sk); |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | |
| 503 | /* |
| 504 | * Set the maximum length of sync message according to the |
| 505 | * specified interface's MTU. |
| 506 | */ |
| 507 | static int set_sync_mesg_maxlen(int sync_state) |
| 508 | { |
| 509 | struct net_device *dev; |
| 510 | int num; |
| 511 | |
| 512 | if (sync_state == IP_VS_STATE_MASTER) { |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 513 | if ((dev = __dev_get_by_name(&init_net, ip_vs_master_mcast_ifn)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | return -ENODEV; |
| 515 | |
| 516 | num = (dev->mtu - sizeof(struct iphdr) - |
| 517 | sizeof(struct udphdr) - |
| 518 | SYNC_MESG_HEADER_LEN - 20) / SIMPLE_CONN_SIZE; |
| 519 | sync_send_mesg_maxlen = |
| 520 | SYNC_MESG_HEADER_LEN + SIMPLE_CONN_SIZE * num; |
| 521 | IP_VS_DBG(7, "setting the maximum length of sync sending " |
| 522 | "message %d.\n", sync_send_mesg_maxlen); |
| 523 | } else if (sync_state == IP_VS_STATE_BACKUP) { |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 524 | if ((dev = __dev_get_by_name(&init_net, ip_vs_backup_mcast_ifn)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | return -ENODEV; |
| 526 | |
| 527 | sync_recv_mesg_maxlen = dev->mtu - |
| 528 | sizeof(struct iphdr) - sizeof(struct udphdr); |
| 529 | IP_VS_DBG(7, "setting the maximum length of sync receiving " |
| 530 | "message %d.\n", sync_recv_mesg_maxlen); |
| 531 | } |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | |
| 537 | /* |
| 538 | * Join a multicast group. |
| 539 | * the group is specified by a class D multicast address 224.0.0.0/8 |
| 540 | * in the in_addr structure passed in as a parameter. |
| 541 | */ |
| 542 | static int |
| 543 | join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname) |
| 544 | { |
| 545 | struct ip_mreqn mreq; |
| 546 | struct net_device *dev; |
| 547 | int ret; |
| 548 | |
| 549 | memset(&mreq, 0, sizeof(mreq)); |
| 550 | memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr)); |
| 551 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 552 | if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | return -ENODEV; |
| 554 | if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if) |
| 555 | return -EINVAL; |
| 556 | |
| 557 | mreq.imr_ifindex = dev->ifindex; |
| 558 | |
| 559 | lock_sock(sk); |
| 560 | ret = ip_mc_join_group(sk, &mreq); |
| 561 | release_sock(sk); |
| 562 | |
| 563 | return ret; |
| 564 | } |
| 565 | |
| 566 | |
| 567 | static int bind_mcastif_addr(struct socket *sock, char *ifname) |
| 568 | { |
| 569 | struct net_device *dev; |
Al Viro | a61ced5 | 2006-09-26 21:27:54 -0700 | [diff] [blame] | 570 | __be32 addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | struct sockaddr_in sin; |
| 572 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 573 | if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | return -ENODEV; |
| 575 | |
| 576 | addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE); |
| 577 | if (!addr) |
| 578 | IP_VS_ERR("You probably need to specify IP address on " |
| 579 | "multicast interface.\n"); |
| 580 | |
| 581 | IP_VS_DBG(7, "binding socket with (%s) %u.%u.%u.%u\n", |
| 582 | ifname, NIPQUAD(addr)); |
| 583 | |
| 584 | /* Now bind the socket with the address of multicast interface */ |
| 585 | sin.sin_family = AF_INET; |
| 586 | sin.sin_addr.s_addr = addr; |
| 587 | sin.sin_port = 0; |
| 588 | |
| 589 | return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin)); |
| 590 | } |
| 591 | |
| 592 | /* |
| 593 | * Set up sending multicast socket over UDP |
| 594 | */ |
| 595 | static struct socket * make_send_sock(void) |
| 596 | { |
| 597 | struct socket *sock; |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 598 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
| 600 | /* First create a socket */ |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 601 | result = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock); |
| 602 | if (result < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | IP_VS_ERR("Error during creation of socket; terminating\n"); |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 604 | return ERR_PTR(result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 607 | result = set_mcast_if(sock->sk, ip_vs_master_mcast_ifn); |
| 608 | if (result < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | IP_VS_ERR("Error setting outbound mcast interface\n"); |
| 610 | goto error; |
| 611 | } |
| 612 | |
| 613 | set_mcast_loop(sock->sk, 0); |
| 614 | set_mcast_ttl(sock->sk, 1); |
| 615 | |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 616 | result = bind_mcastif_addr(sock, ip_vs_master_mcast_ifn); |
| 617 | if (result < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | IP_VS_ERR("Error binding address of the mcast interface\n"); |
| 619 | goto error; |
| 620 | } |
| 621 | |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 622 | result = sock->ops->connect(sock, (struct sockaddr *) &mcast_addr, |
| 623 | sizeof(struct sockaddr), 0); |
| 624 | if (result < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | IP_VS_ERR("Error connecting to the multicast addr\n"); |
| 626 | goto error; |
| 627 | } |
| 628 | |
| 629 | return sock; |
| 630 | |
| 631 | error: |
| 632 | sock_release(sock); |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 633 | return ERR_PTR(result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | |
| 637 | /* |
| 638 | * Set up receiving multicast socket over UDP |
| 639 | */ |
| 640 | static struct socket * make_receive_sock(void) |
| 641 | { |
| 642 | struct socket *sock; |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 643 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
| 645 | /* First create a socket */ |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 646 | result = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock); |
| 647 | if (result < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | IP_VS_ERR("Error during creation of socket; terminating\n"); |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 649 | return ERR_PTR(result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /* it is equivalent to the REUSEADDR option in user-space */ |
| 653 | sock->sk->sk_reuse = 1; |
| 654 | |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 655 | result = sock->ops->bind(sock, (struct sockaddr *) &mcast_addr, |
| 656 | sizeof(struct sockaddr)); |
| 657 | if (result < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | IP_VS_ERR("Error binding to the multicast addr\n"); |
| 659 | goto error; |
| 660 | } |
| 661 | |
| 662 | /* join the multicast group */ |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 663 | result = join_mcast_group(sock->sk, |
| 664 | (struct in_addr *) &mcast_addr.sin_addr, |
| 665 | ip_vs_backup_mcast_ifn); |
| 666 | if (result < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | IP_VS_ERR("Error joining to the multicast group\n"); |
| 668 | goto error; |
| 669 | } |
| 670 | |
| 671 | return sock; |
| 672 | |
| 673 | error: |
| 674 | sock_release(sock); |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 675 | return ERR_PTR(result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | |
| 679 | static int |
| 680 | ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length) |
| 681 | { |
| 682 | struct msghdr msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL}; |
| 683 | struct kvec iov; |
| 684 | int len; |
| 685 | |
| 686 | EnterFunction(7); |
| 687 | iov.iov_base = (void *)buffer; |
| 688 | iov.iov_len = length; |
| 689 | |
| 690 | len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length)); |
| 691 | |
| 692 | LeaveFunction(7); |
| 693 | return len; |
| 694 | } |
| 695 | |
| 696 | static void |
| 697 | ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg) |
| 698 | { |
| 699 | int msize; |
| 700 | |
| 701 | msize = msg->size; |
| 702 | |
| 703 | /* Put size in network byte order */ |
| 704 | msg->size = htons(msg->size); |
| 705 | |
| 706 | if (ip_vs_send_async(sock, (char *)msg, msize) != msize) |
| 707 | IP_VS_ERR("ip_vs_send_async error\n"); |
| 708 | } |
| 709 | |
| 710 | static int |
| 711 | ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen) |
| 712 | { |
| 713 | struct msghdr msg = {NULL,}; |
| 714 | struct kvec iov; |
| 715 | int len; |
| 716 | |
| 717 | EnterFunction(7); |
| 718 | |
| 719 | /* Receive a packet */ |
| 720 | iov.iov_base = buffer; |
| 721 | iov.iov_len = (size_t)buflen; |
| 722 | |
| 723 | len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, 0); |
| 724 | |
| 725 | if (len < 0) |
| 726 | return -1; |
| 727 | |
| 728 | LeaveFunction(7); |
| 729 | return len; |
| 730 | } |
| 731 | |
| 732 | |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 733 | static int sync_thread_master(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | { |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 735 | struct ip_vs_sync_thread_data *tinfo = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | struct ip_vs_sync_buff *sb; |
| 737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, " |
| 739 | "syncid = %d\n", |
| 740 | ip_vs_master_mcast_ifn, ip_vs_master_syncid); |
| 741 | |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 742 | while (!kthread_should_stop()) { |
| 743 | while ((sb = sb_dequeue())) { |
| 744 | ip_vs_send_sync_msg(tinfo->sock, sb->mesg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | ip_vs_sync_buff_release(sb); |
| 746 | } |
| 747 | |
| 748 | /* check if entries stay in curr_sb for 2 seconds */ |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 749 | sb = get_curr_sync_buff(2 * HZ); |
| 750 | if (sb) { |
| 751 | ip_vs_send_sync_msg(tinfo->sock, sb->mesg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | ip_vs_sync_buff_release(sb); |
| 753 | } |
| 754 | |
Sven Wegener | 375c6bb | 2008-07-16 11:14:03 +0000 | [diff] [blame] | 755 | schedule_timeout_interruptible(HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | /* clean up the sync_buff queue */ |
| 759 | while ((sb=sb_dequeue())) { |
| 760 | ip_vs_sync_buff_release(sb); |
| 761 | } |
| 762 | |
| 763 | /* clean up the current sync_buff */ |
| 764 | if ((sb = get_curr_sync_buff(0))) { |
| 765 | ip_vs_sync_buff_release(sb); |
| 766 | } |
| 767 | |
| 768 | /* release the sending multicast socket */ |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 769 | sock_release(tinfo->sock); |
| 770 | kfree(tinfo); |
| 771 | |
| 772 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 776 | static int sync_thread_backup(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | { |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 778 | struct ip_vs_sync_thread_data *tinfo = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | int len; |
| 780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, " |
| 782 | "syncid = %d\n", |
| 783 | ip_vs_backup_mcast_ifn, ip_vs_backup_syncid); |
| 784 | |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 785 | while (!kthread_should_stop()) { |
Sven Wegener | ba6fd85 | 2008-07-16 11:13:56 +0000 | [diff] [blame] | 786 | wait_event_interruptible(*tinfo->sock->sk->sk_sleep, |
| 787 | !skb_queue_empty(&tinfo->sock->sk->sk_receive_queue) |
| 788 | || kthread_should_stop()); |
| 789 | |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 790 | /* do we have data now? */ |
| 791 | while (!skb_queue_empty(&(tinfo->sock->sk->sk_receive_queue))) { |
| 792 | len = ip_vs_receive(tinfo->sock, tinfo->buf, |
| 793 | sync_recv_mesg_maxlen); |
| 794 | if (len <= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | IP_VS_ERR("receiving message error\n"); |
| 796 | break; |
| 797 | } |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 798 | |
| 799 | /* disable bottom half, because it accesses the data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | shared by softirq while getting/creating conns */ |
| 801 | local_bh_disable(); |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 802 | ip_vs_process_message(tinfo->buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | local_bh_enable(); |
| 804 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | /* release the sending multicast socket */ |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 808 | sock_release(tinfo->sock); |
| 809 | kfree(tinfo->buf); |
Neil Horman | cc0191a | 2007-06-18 22:33:20 -0700 | [diff] [blame] | 810 | kfree(tinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | |
| 816 | int start_sync_thread(int state, char *mcast_ifn, __u8 syncid) |
| 817 | { |
Neil Horman | cc0191a | 2007-06-18 22:33:20 -0700 | [diff] [blame] | 818 | struct ip_vs_sync_thread_data *tinfo; |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 819 | struct task_struct **realtask, *task; |
| 820 | struct socket *sock; |
| 821 | char *name, *buf = NULL; |
| 822 | int (*threadfn)(void *data); |
| 823 | int result = -ENOMEM; |
Neil Horman | cc0191a | 2007-06-18 22:33:20 -0700 | [diff] [blame] | 824 | |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 825 | IP_VS_DBG(7, "%s: pid %d\n", __func__, task_pid_nr(current)); |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 826 | IP_VS_DBG(7, "Each ip_vs_sync_conn entry needs %Zd bytes\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | sizeof(struct ip_vs_sync_conn)); |
| 828 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | if (state == IP_VS_STATE_MASTER) { |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 830 | if (sync_master_thread) |
| 831 | return -EEXIST; |
| 832 | |
Simon Horman | 37004af | 2006-12-10 22:36:18 -0800 | [diff] [blame] | 833 | strlcpy(ip_vs_master_mcast_ifn, mcast_ifn, |
| 834 | sizeof(ip_vs_master_mcast_ifn)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | ip_vs_master_syncid = syncid; |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 836 | realtask = &sync_master_thread; |
| 837 | name = "ipvs_syncmaster"; |
| 838 | threadfn = sync_thread_master; |
| 839 | sock = make_send_sock(); |
| 840 | } else if (state == IP_VS_STATE_BACKUP) { |
| 841 | if (sync_backup_thread) |
| 842 | return -EEXIST; |
| 843 | |
Simon Horman | 37004af | 2006-12-10 22:36:18 -0800 | [diff] [blame] | 844 | strlcpy(ip_vs_backup_mcast_ifn, mcast_ifn, |
| 845 | sizeof(ip_vs_backup_mcast_ifn)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | ip_vs_backup_syncid = syncid; |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 847 | realtask = &sync_backup_thread; |
| 848 | name = "ipvs_syncbackup"; |
| 849 | threadfn = sync_thread_backup; |
| 850 | sock = make_receive_sock(); |
| 851 | } else { |
| 852 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | } |
| 854 | |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 855 | if (IS_ERR(sock)) { |
| 856 | result = PTR_ERR(sock); |
| 857 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 860 | set_sync_mesg_maxlen(state); |
| 861 | if (state == IP_VS_STATE_BACKUP) { |
| 862 | buf = kmalloc(sync_recv_mesg_maxlen, GFP_KERNEL); |
| 863 | if (!buf) |
| 864 | goto outsocket; |
| 865 | } |
| 866 | |
| 867 | tinfo = kmalloc(sizeof(*tinfo), GFP_KERNEL); |
| 868 | if (!tinfo) |
| 869 | goto outbuf; |
| 870 | |
| 871 | tinfo->sock = sock; |
| 872 | tinfo->buf = buf; |
| 873 | |
| 874 | task = kthread_run(threadfn, tinfo, name); |
| 875 | if (IS_ERR(task)) { |
| 876 | result = PTR_ERR(task); |
| 877 | goto outtinfo; |
| 878 | } |
| 879 | |
| 880 | /* mark as active */ |
| 881 | *realtask = task; |
| 882 | ip_vs_sync_state |= state; |
| 883 | |
| 884 | /* increase the module use count */ |
| 885 | ip_vs_use_count_inc(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
| 887 | return 0; |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 888 | |
| 889 | outtinfo: |
| 890 | kfree(tinfo); |
| 891 | outbuf: |
| 892 | kfree(buf); |
| 893 | outsocket: |
| 894 | sock_release(sock); |
| 895 | out: |
| 896 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | |
| 900 | int stop_sync_thread(int state) |
| 901 | { |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 902 | IP_VS_DBG(7, "%s: pid %d\n", __func__, task_pid_nr(current)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 904 | if (state == IP_VS_STATE_MASTER) { |
| 905 | if (!sync_master_thread) |
| 906 | return -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 908 | IP_VS_INFO("stopping master sync thread %d ...\n", |
| 909 | task_pid_nr(sync_master_thread)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 911 | /* |
| 912 | * The lock synchronizes with sb_queue_tail(), so that we don't |
| 913 | * add sync buffers to the queue, when we are already in |
| 914 | * progress of stopping the master sync daemon. |
| 915 | */ |
| 916 | |
Sven Wegener | bc0fde2 | 2008-08-10 09:14:05 +0000 | [diff] [blame] | 917 | spin_lock_bh(&ip_vs_sync_lock); |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 918 | ip_vs_sync_state &= ~IP_VS_STATE_MASTER; |
Sven Wegener | bc0fde2 | 2008-08-10 09:14:05 +0000 | [diff] [blame] | 919 | spin_unlock_bh(&ip_vs_sync_lock); |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 920 | kthread_stop(sync_master_thread); |
| 921 | sync_master_thread = NULL; |
| 922 | } else if (state == IP_VS_STATE_BACKUP) { |
| 923 | if (!sync_backup_thread) |
| 924 | return -ESRCH; |
| 925 | |
| 926 | IP_VS_INFO("stopping backup sync thread %d ...\n", |
| 927 | task_pid_nr(sync_backup_thread)); |
| 928 | |
| 929 | ip_vs_sync_state &= ~IP_VS_STATE_BACKUP; |
| 930 | kthread_stop(sync_backup_thread); |
| 931 | sync_backup_thread = NULL; |
| 932 | } else { |
| 933 | return -EINVAL; |
| 934 | } |
| 935 | |
| 936 | /* decrease the module use count */ |
| 937 | ip_vs_use_count_dec(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | |
| 939 | return 0; |
| 940 | } |