blob: b1694d67abb99b966fd76ef461a9fd94c561c32b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
8 * Version: $Id: ip_vs_sync.c,v 1.13 2003/06/08 09:31:19 wensong Exp $
9 *
10 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
11 *
12 * ip_vs_sync: sync connection info from master load balancer to backups
13 * through multicast
14 *
15 * Changes:
16 * Alexandre Cassen : Added master & backup support at a time.
17 * Alexandre Cassen : Added SyncID support for incoming sync
18 * messages filtering.
19 * Justin Ossevoort : Fix endian problem on sync message size.
20 */
21
22#include <linux/module.h>
23#include <linux/slab.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020024#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/net.h>
26#include <linux/completion.h>
27#include <linux/delay.h>
28#include <linux/skbuff.h>
29#include <linux/in.h>
30#include <linux/igmp.h> /* for ip_mc_join_group */
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020031#include <linux/udp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <net/ip.h>
34#include <net/sock.h>
35#include <asm/uaccess.h> /* for get_fs and set_fs */
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 */
46struct ip_vs_sync_conn {
47 __u8 reserved;
48
49 /* Protocol, addresses and port numbers */
50 __u8 protocol; /* Which protocol (TCP/UDP) */
Al Viro014d7302006-09-28 14:29:52 -070051 __be16 cport;
52 __be16 vport;
53 __be16 dport;
54 __be32 caddr; /* client address */
55 __be32 vaddr; /* virtual address */
56 __be32 daddr; /* destination address */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 /* Flags and state transition */
Al Viro014d7302006-09-28 14:29:52 -070059 __be16 flags; /* status flags */
60 __be16 state; /* state info */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 /* The sequence options start here */
63};
64
65struct 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 Hormancc0191a2007-06-18 22:33:20 -070070struct ip_vs_sync_thread_data {
71 struct completion *startup;
72 int state;
73};
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#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
103struct 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 */
112static int sync_send_mesg_maxlen;
113static int sync_recv_mesg_maxlen;
114
115struct 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 */
127static LIST_HEAD(ip_vs_sync_queue);
128static DEFINE_SPINLOCK(ip_vs_sync_lock);
129
130/* current sync_buff for accepting new conn entries */
131static struct ip_vs_sync_buff *curr_sb = NULL;
132static DEFINE_SPINLOCK(curr_sb_lock);
133
134/* ipvs sync daemon state */
135volatile int ip_vs_sync_state = IP_VS_STATE_NONE;
136volatile int ip_vs_master_syncid = 0;
137volatile int ip_vs_backup_syncid = 0;
138
139/* multicast interface name */
140char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
141char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
142
143/* multicast addr */
144static struct sockaddr_in mcast_addr;
145
146
147static inline void sb_queue_tail(struct ip_vs_sync_buff *sb)
148{
149 spin_lock(&ip_vs_sync_lock);
150 list_add_tail(&sb->list, &ip_vs_sync_queue);
151 spin_unlock(&ip_vs_sync_lock);
152}
153
154static inline struct ip_vs_sync_buff * sb_dequeue(void)
155{
156 struct ip_vs_sync_buff *sb;
157
158 spin_lock_bh(&ip_vs_sync_lock);
159 if (list_empty(&ip_vs_sync_queue)) {
160 sb = NULL;
161 } else {
162 sb = list_entry(ip_vs_sync_queue.next,
163 struct ip_vs_sync_buff,
164 list);
165 list_del(&sb->list);
166 }
167 spin_unlock_bh(&ip_vs_sync_lock);
168
169 return sb;
170}
171
172static inline struct ip_vs_sync_buff * ip_vs_sync_buff_create(void)
173{
174 struct ip_vs_sync_buff *sb;
175
176 if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
177 return NULL;
178
179 if (!(sb->mesg=kmalloc(sync_send_mesg_maxlen, GFP_ATOMIC))) {
180 kfree(sb);
181 return NULL;
182 }
183 sb->mesg->nr_conns = 0;
184 sb->mesg->syncid = ip_vs_master_syncid;
185 sb->mesg->size = 4;
186 sb->head = (unsigned char *)sb->mesg + 4;
187 sb->end = (unsigned char *)sb->mesg + sync_send_mesg_maxlen;
188 sb->firstuse = jiffies;
189 return sb;
190}
191
192static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff *sb)
193{
194 kfree(sb->mesg);
195 kfree(sb);
196}
197
198/*
199 * Get the current sync buffer if it has been created for more
200 * than the specified time or the specified time is zero.
201 */
202static inline struct ip_vs_sync_buff *
203get_curr_sync_buff(unsigned long time)
204{
205 struct ip_vs_sync_buff *sb;
206
207 spin_lock_bh(&curr_sb_lock);
208 if (curr_sb && (time == 0 ||
209 time_before(jiffies - curr_sb->firstuse, time))) {
210 sb = curr_sb;
211 curr_sb = NULL;
212 } else
213 sb = NULL;
214 spin_unlock_bh(&curr_sb_lock);
215 return sb;
216}
217
218
219/*
220 * Add an ip_vs_conn information into the current sync_buff.
221 * Called by ip_vs_in.
222 */
223void ip_vs_sync_conn(struct ip_vs_conn *cp)
224{
225 struct ip_vs_sync_mesg *m;
226 struct ip_vs_sync_conn *s;
227 int len;
228
229 spin_lock(&curr_sb_lock);
230 if (!curr_sb) {
231 if (!(curr_sb=ip_vs_sync_buff_create())) {
232 spin_unlock(&curr_sb_lock);
233 IP_VS_ERR("ip_vs_sync_buff_create failed.\n");
234 return;
235 }
236 }
237
238 len = (cp->flags & IP_VS_CONN_F_SEQ_MASK) ? FULL_CONN_SIZE :
239 SIMPLE_CONN_SIZE;
240 m = curr_sb->mesg;
241 s = (struct ip_vs_sync_conn *)curr_sb->head;
242
243 /* copy members */
244 s->protocol = cp->protocol;
245 s->cport = cp->cport;
246 s->vport = cp->vport;
247 s->dport = cp->dport;
248 s->caddr = cp->caddr;
249 s->vaddr = cp->vaddr;
250 s->daddr = cp->daddr;
251 s->flags = htons(cp->flags & ~IP_VS_CONN_F_HASHED);
252 s->state = htons(cp->state);
253 if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
254 struct ip_vs_sync_conn_options *opt =
255 (struct ip_vs_sync_conn_options *)&s[1];
256 memcpy(opt, &cp->in_seq, sizeof(*opt));
257 }
258
259 m->nr_conns++;
260 m->size += len;
261 curr_sb->head += len;
262
263 /* check if there is a space for next one */
264 if (curr_sb->head+FULL_CONN_SIZE > curr_sb->end) {
265 sb_queue_tail(curr_sb);
266 curr_sb = NULL;
267 }
268 spin_unlock(&curr_sb_lock);
269
270 /* synchronize its controller if it has */
271 if (cp->control)
272 ip_vs_sync_conn(cp->control);
273}
274
275
276/*
277 * Process received multicast message and create the corresponding
278 * ip_vs_conn entries.
279 */
280static void ip_vs_process_message(const char *buffer, const size_t buflen)
281{
282 struct ip_vs_sync_mesg *m = (struct ip_vs_sync_mesg *)buffer;
283 struct ip_vs_sync_conn *s;
284 struct ip_vs_sync_conn_options *opt;
285 struct ip_vs_conn *cp;
Andy Gospodarek5c818332007-10-29 04:35:45 -0700286 struct ip_vs_protocol *pp;
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800287 struct ip_vs_dest *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 char *p;
289 int i;
290
291 /* Convert size back to host byte order */
292 m->size = ntohs(m->size);
293
294 if (buflen != m->size) {
295 IP_VS_ERR("bogus message\n");
296 return;
297 }
298
299 /* SyncID sanity check */
300 if (ip_vs_backup_syncid != 0 && m->syncid != ip_vs_backup_syncid) {
301 IP_VS_DBG(7, "Ignoring incoming msg with syncid = %d\n",
302 m->syncid);
303 return;
304 }
305
306 p = (char *)buffer + sizeof(struct ip_vs_sync_mesg);
307 for (i=0; i<m->nr_conns; i++) {
Julian Anastasov87375ab2005-09-14 21:08:51 -0700308 unsigned flags;
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 s = (struct ip_vs_sync_conn *)p;
Julian Anastasov87375ab2005-09-14 21:08:51 -0700311 flags = ntohs(s->flags);
312 if (!(flags & IP_VS_CONN_F_TEMPLATE))
313 cp = ip_vs_conn_in_get(s->protocol,
314 s->caddr, s->cport,
315 s->vaddr, s->vport);
316 else
317 cp = ip_vs_ct_in_get(s->protocol,
318 s->caddr, s->cport,
319 s->vaddr, s->vport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (!cp) {
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800321 /*
322 * Find the appropriate destination for the connection.
323 * If it is not found the connection will remain unbound
324 * but still handled.
325 */
326 dest = ip_vs_find_dest(s->daddr, s->dport,
327 s->vaddr, s->vport,
328 s->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 cp = ip_vs_conn_new(s->protocol,
330 s->caddr, s->cport,
331 s->vaddr, s->vport,
332 s->daddr, s->dport,
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800333 flags, dest);
334 if (dest)
335 atomic_dec(&dest->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (!cp) {
337 IP_VS_ERR("ip_vs_conn_new failed\n");
338 return;
339 }
340 cp->state = ntohs(s->state);
341 } else if (!cp->dest) {
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800342 dest = ip_vs_try_bind_dest(cp);
343 if (!dest) {
344 /* it is an unbound entry created by
345 * synchronization */
346 cp->state = ntohs(s->state);
347 cp->flags = flags | IP_VS_CONN_F_HASHED;
348 } else
349 atomic_dec(&dest->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 } /* Note that we don't touch its state and flags
351 if it is a normal entry. */
352
Julian Anastasov87375ab2005-09-14 21:08:51 -0700353 if (flags & IP_VS_CONN_F_SEQ_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 opt = (struct ip_vs_sync_conn_options *)&s[1];
355 memcpy(&cp->in_seq, opt, sizeof(*opt));
356 p += FULL_CONN_SIZE;
357 } else
358 p += SIMPLE_CONN_SIZE;
359
360 atomic_set(&cp->in_pkts, sysctl_ip_vs_sync_threshold[0]);
Andy Gospodarek5c818332007-10-29 04:35:45 -0700361 pp = ip_vs_proto_get(s->protocol);
362 cp->timeout = pp->timeout_table[cp->state];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 ip_vs_conn_put(cp);
364
365 if (p > buffer+buflen) {
366 IP_VS_ERR("bogus message\n");
367 return;
368 }
369 }
370}
371
372
373/*
374 * Setup loopback of outgoing multicasts on a sending socket
375 */
376static void set_mcast_loop(struct sock *sk, u_char loop)
377{
378 struct inet_sock *inet = inet_sk(sk);
379
380 /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
381 lock_sock(sk);
382 inet->mc_loop = loop ? 1 : 0;
383 release_sock(sk);
384}
385
386/*
387 * Specify TTL for outgoing multicasts on a sending socket
388 */
389static void set_mcast_ttl(struct sock *sk, u_char ttl)
390{
391 struct inet_sock *inet = inet_sk(sk);
392
393 /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
394 lock_sock(sk);
395 inet->mc_ttl = ttl;
396 release_sock(sk);
397}
398
399/*
400 * Specifiy default interface for outgoing multicasts
401 */
402static int set_mcast_if(struct sock *sk, char *ifname)
403{
404 struct net_device *dev;
405 struct inet_sock *inet = inet_sk(sk);
406
Eric W. Biederman881d9662007-09-17 11:56:21 -0700407 if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return -ENODEV;
409
410 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
411 return -EINVAL;
412
413 lock_sock(sk);
414 inet->mc_index = dev->ifindex;
415 /* inet->mc_addr = 0; */
416 release_sock(sk);
417
418 return 0;
419}
420
421
422/*
423 * Set the maximum length of sync message according to the
424 * specified interface's MTU.
425 */
426static int set_sync_mesg_maxlen(int sync_state)
427{
428 struct net_device *dev;
429 int num;
430
431 if (sync_state == IP_VS_STATE_MASTER) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700432 if ((dev = __dev_get_by_name(&init_net, ip_vs_master_mcast_ifn)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return -ENODEV;
434
435 num = (dev->mtu - sizeof(struct iphdr) -
436 sizeof(struct udphdr) -
437 SYNC_MESG_HEADER_LEN - 20) / SIMPLE_CONN_SIZE;
438 sync_send_mesg_maxlen =
439 SYNC_MESG_HEADER_LEN + SIMPLE_CONN_SIZE * num;
440 IP_VS_DBG(7, "setting the maximum length of sync sending "
441 "message %d.\n", sync_send_mesg_maxlen);
442 } else if (sync_state == IP_VS_STATE_BACKUP) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700443 if ((dev = __dev_get_by_name(&init_net, ip_vs_backup_mcast_ifn)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return -ENODEV;
445
446 sync_recv_mesg_maxlen = dev->mtu -
447 sizeof(struct iphdr) - sizeof(struct udphdr);
448 IP_VS_DBG(7, "setting the maximum length of sync receiving "
449 "message %d.\n", sync_recv_mesg_maxlen);
450 }
451
452 return 0;
453}
454
455
456/*
457 * Join a multicast group.
458 * the group is specified by a class D multicast address 224.0.0.0/8
459 * in the in_addr structure passed in as a parameter.
460 */
461static int
462join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
463{
464 struct ip_mreqn mreq;
465 struct net_device *dev;
466 int ret;
467
468 memset(&mreq, 0, sizeof(mreq));
469 memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
470
Eric W. Biederman881d9662007-09-17 11:56:21 -0700471 if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return -ENODEV;
473 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
474 return -EINVAL;
475
476 mreq.imr_ifindex = dev->ifindex;
477
478 lock_sock(sk);
479 ret = ip_mc_join_group(sk, &mreq);
480 release_sock(sk);
481
482 return ret;
483}
484
485
486static int bind_mcastif_addr(struct socket *sock, char *ifname)
487{
488 struct net_device *dev;
Al Viroa61ced52006-09-26 21:27:54 -0700489 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 struct sockaddr_in sin;
491
Eric W. Biederman881d9662007-09-17 11:56:21 -0700492 if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return -ENODEV;
494
495 addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
496 if (!addr)
497 IP_VS_ERR("You probably need to specify IP address on "
498 "multicast interface.\n");
499
500 IP_VS_DBG(7, "binding socket with (%s) %u.%u.%u.%u\n",
501 ifname, NIPQUAD(addr));
502
503 /* Now bind the socket with the address of multicast interface */
504 sin.sin_family = AF_INET;
505 sin.sin_addr.s_addr = addr;
506 sin.sin_port = 0;
507
508 return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
509}
510
511/*
512 * Set up sending multicast socket over UDP
513 */
514static struct socket * make_send_sock(void)
515{
516 struct socket *sock;
517
518 /* First create a socket */
519 if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
520 IP_VS_ERR("Error during creation of socket; terminating\n");
521 return NULL;
522 }
523
524 if (set_mcast_if(sock->sk, ip_vs_master_mcast_ifn) < 0) {
525 IP_VS_ERR("Error setting outbound mcast interface\n");
526 goto error;
527 }
528
529 set_mcast_loop(sock->sk, 0);
530 set_mcast_ttl(sock->sk, 1);
531
532 if (bind_mcastif_addr(sock, ip_vs_master_mcast_ifn) < 0) {
533 IP_VS_ERR("Error binding address of the mcast interface\n");
534 goto error;
535 }
536
537 if (sock->ops->connect(sock,
538 (struct sockaddr*)&mcast_addr,
539 sizeof(struct sockaddr), 0) < 0) {
540 IP_VS_ERR("Error connecting to the multicast addr\n");
541 goto error;
542 }
543
544 return sock;
545
546 error:
547 sock_release(sock);
548 return NULL;
549}
550
551
552/*
553 * Set up receiving multicast socket over UDP
554 */
555static struct socket * make_receive_sock(void)
556{
557 struct socket *sock;
558
559 /* First create a socket */
560 if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
561 IP_VS_ERR("Error during creation of socket; terminating\n");
562 return NULL;
563 }
564
565 /* it is equivalent to the REUSEADDR option in user-space */
566 sock->sk->sk_reuse = 1;
567
568 if (sock->ops->bind(sock,
569 (struct sockaddr*)&mcast_addr,
570 sizeof(struct sockaddr)) < 0) {
571 IP_VS_ERR("Error binding to the multicast addr\n");
572 goto error;
573 }
574
575 /* join the multicast group */
576 if (join_mcast_group(sock->sk,
577 (struct in_addr*)&mcast_addr.sin_addr,
578 ip_vs_backup_mcast_ifn) < 0) {
579 IP_VS_ERR("Error joining to the multicast group\n");
580 goto error;
581 }
582
583 return sock;
584
585 error:
586 sock_release(sock);
587 return NULL;
588}
589
590
591static int
592ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
593{
594 struct msghdr msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
595 struct kvec iov;
596 int len;
597
598 EnterFunction(7);
599 iov.iov_base = (void *)buffer;
600 iov.iov_len = length;
601
602 len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
603
604 LeaveFunction(7);
605 return len;
606}
607
608static void
609ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
610{
611 int msize;
612
613 msize = msg->size;
614
615 /* Put size in network byte order */
616 msg->size = htons(msg->size);
617
618 if (ip_vs_send_async(sock, (char *)msg, msize) != msize)
619 IP_VS_ERR("ip_vs_send_async error\n");
620}
621
622static int
623ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
624{
625 struct msghdr msg = {NULL,};
626 struct kvec iov;
627 int len;
628
629 EnterFunction(7);
630
631 /* Receive a packet */
632 iov.iov_base = buffer;
633 iov.iov_len = (size_t)buflen;
634
635 len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, 0);
636
637 if (len < 0)
638 return -1;
639
640 LeaveFunction(7);
641 return len;
642}
643
644
645static DECLARE_WAIT_QUEUE_HEAD(sync_wait);
646static pid_t sync_master_pid = 0;
647static pid_t sync_backup_pid = 0;
648
649static DECLARE_WAIT_QUEUE_HEAD(stop_sync_wait);
650static int stop_master_sync = 0;
651static int stop_backup_sync = 0;
652
653static void sync_master_loop(void)
654{
655 struct socket *sock;
656 struct ip_vs_sync_buff *sb;
657
658 /* create the sending multicast socket */
659 sock = make_send_sock();
660 if (!sock)
661 return;
662
663 IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, "
664 "syncid = %d\n",
665 ip_vs_master_mcast_ifn, ip_vs_master_syncid);
666
667 for (;;) {
668 while ((sb=sb_dequeue())) {
669 ip_vs_send_sync_msg(sock, sb->mesg);
670 ip_vs_sync_buff_release(sb);
671 }
672
673 /* check if entries stay in curr_sb for 2 seconds */
674 if ((sb = get_curr_sync_buff(2*HZ))) {
675 ip_vs_send_sync_msg(sock, sb->mesg);
676 ip_vs_sync_buff_release(sb);
677 }
678
679 if (stop_master_sync)
680 break;
681
Simon Horman89eaeb02006-12-10 22:35:24 -0800682 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
685 /* clean up the sync_buff queue */
686 while ((sb=sb_dequeue())) {
687 ip_vs_sync_buff_release(sb);
688 }
689
690 /* clean up the current sync_buff */
691 if ((sb = get_curr_sync_buff(0))) {
692 ip_vs_sync_buff_release(sb);
693 }
694
695 /* release the sending multicast socket */
696 sock_release(sock);
697}
698
699
700static void sync_backup_loop(void)
701{
702 struct socket *sock;
703 char *buf;
704 int len;
705
706 if (!(buf = kmalloc(sync_recv_mesg_maxlen, GFP_ATOMIC))) {
707 IP_VS_ERR("sync_backup_loop: kmalloc error\n");
708 return;
709 }
710
711 /* create the receiving multicast socket */
712 sock = make_receive_sock();
713 if (!sock)
714 goto out;
715
716 IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
717 "syncid = %d\n",
718 ip_vs_backup_mcast_ifn, ip_vs_backup_syncid);
719
720 for (;;) {
721 /* do you have data now? */
722 while (!skb_queue_empty(&(sock->sk->sk_receive_queue))) {
723 if ((len =
724 ip_vs_receive(sock, buf,
725 sync_recv_mesg_maxlen)) <= 0) {
726 IP_VS_ERR("receiving message error\n");
727 break;
728 }
729 /* disable bottom half, because it accessed the data
730 shared by softirq while getting/creating conns */
731 local_bh_disable();
732 ip_vs_process_message(buf, len);
733 local_bh_enable();
734 }
735
736 if (stop_backup_sync)
737 break;
738
Simon Horman89eaeb02006-12-10 22:35:24 -0800739 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741
742 /* release the sending multicast socket */
743 sock_release(sock);
744
745 out:
746 kfree(buf);
747}
748
749
750static void set_sync_pid(int sync_state, pid_t sync_pid)
751{
752 if (sync_state == IP_VS_STATE_MASTER)
753 sync_master_pid = sync_pid;
754 else if (sync_state == IP_VS_STATE_BACKUP)
755 sync_backup_pid = sync_pid;
756}
757
758static void set_stop_sync(int sync_state, int set)
759{
760 if (sync_state == IP_VS_STATE_MASTER)
761 stop_master_sync = set;
762 else if (sync_state == IP_VS_STATE_BACKUP)
763 stop_backup_sync = set;
764 else {
765 stop_master_sync = set;
766 stop_backup_sync = set;
767 }
768}
769
770static int sync_thread(void *startup)
771{
772 DECLARE_WAITQUEUE(wait, current);
773 mm_segment_t oldmm;
774 int state;
775 const char *name;
Neil Hormancc0191a2007-06-18 22:33:20 -0700776 struct ip_vs_sync_thread_data *tinfo = startup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 /* increase the module use count */
779 ip_vs_use_count_inc();
780
781 if (ip_vs_sync_state & IP_VS_STATE_MASTER && !sync_master_pid) {
782 state = IP_VS_STATE_MASTER;
783 name = "ipvs_syncmaster";
784 } else if (ip_vs_sync_state & IP_VS_STATE_BACKUP && !sync_backup_pid) {
785 state = IP_VS_STATE_BACKUP;
786 name = "ipvs_syncbackup";
787 } else {
788 IP_VS_BUG();
789 ip_vs_use_count_dec();
790 return -EINVAL;
791 }
792
793 daemonize(name);
794
795 oldmm = get_fs();
796 set_fs(KERNEL_DS);
797
798 /* Block all signals */
799 spin_lock_irq(&current->sighand->siglock);
800 siginitsetinv(&current->blocked, 0);
801 recalc_sigpending();
802 spin_unlock_irq(&current->sighand->siglock);
803
804 /* set the maximum length of sync message */
805 set_sync_mesg_maxlen(state);
806
807 /* set up multicast address */
808 mcast_addr.sin_family = AF_INET;
809 mcast_addr.sin_port = htons(IP_VS_SYNC_PORT);
810 mcast_addr.sin_addr.s_addr = htonl(IP_VS_SYNC_GROUP);
811
812 add_wait_queue(&sync_wait, &wait);
813
Pavel Emelyanov6651fd52007-10-18 23:40:43 -0700814 set_sync_pid(state, task_pid_nr(current));
Neil Hormancc0191a2007-06-18 22:33:20 -0700815 complete(tinfo->startup);
816
817 /*
818 * once we call the completion queue above, we should
819 * null out that reference, since its allocated on the
820 * stack of the creating kernel thread
821 */
822 tinfo->startup = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 /* processing master/backup loop here */
825 if (state == IP_VS_STATE_MASTER)
826 sync_master_loop();
827 else if (state == IP_VS_STATE_BACKUP)
828 sync_backup_loop();
829 else IP_VS_BUG();
830
831 remove_wait_queue(&sync_wait, &wait);
832
833 /* thread exits */
Neil Hormancc0191a2007-06-18 22:33:20 -0700834
835 /*
836 * If we weren't explicitly stopped, then we
837 * exited in error, and should undo our state
838 */
839 if ((!stop_master_sync) && (!stop_backup_sync))
840 ip_vs_sync_state -= tinfo->state;
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 set_sync_pid(state, 0);
843 IP_VS_INFO("sync thread stopped!\n");
844
845 set_fs(oldmm);
846
847 /* decrease the module use count */
848 ip_vs_use_count_dec();
849
850 set_stop_sync(state, 0);
851 wake_up(&stop_sync_wait);
852
Neil Hormancc0191a2007-06-18 22:33:20 -0700853 /*
854 * we need to free the structure that was allocated
855 * for us in start_sync_thread
856 */
857 kfree(tinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return 0;
859}
860
861
862static int fork_sync_thread(void *startup)
863{
864 pid_t pid;
865
866 /* fork the sync thread here, then the parent process of the
867 sync thread is the init process after this thread exits. */
868 repeat:
869 if ((pid = kernel_thread(sync_thread, startup, 0)) < 0) {
870 IP_VS_ERR("could not create sync_thread due to %d... "
871 "retrying.\n", pid);
Simon Horman89eaeb02006-12-10 22:35:24 -0800872 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 goto repeat;
874 }
875
876 return 0;
877}
878
879
880int start_sync_thread(int state, char *mcast_ifn, __u8 syncid)
881{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700882 DECLARE_COMPLETION_ONSTACK(startup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 pid_t pid;
Neil Hormancc0191a2007-06-18 22:33:20 -0700884 struct ip_vs_sync_thread_data *tinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 if ((state == IP_VS_STATE_MASTER && sync_master_pid) ||
887 (state == IP_VS_STATE_BACKUP && sync_backup_pid))
888 return -EEXIST;
889
Neil Hormancc0191a2007-06-18 22:33:20 -0700890 /*
891 * Note that tinfo will be freed in sync_thread on exit
892 */
893 tinfo = kmalloc(sizeof(struct ip_vs_sync_thread_data), GFP_KERNEL);
894 if (!tinfo)
895 return -ENOMEM;
896
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700897 IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 IP_VS_DBG(7, "Each ip_vs_sync_conn entry need %Zd bytes\n",
899 sizeof(struct ip_vs_sync_conn));
900
901 ip_vs_sync_state |= state;
902 if (state == IP_VS_STATE_MASTER) {
Simon Horman37004af2006-12-10 22:36:18 -0800903 strlcpy(ip_vs_master_mcast_ifn, mcast_ifn,
904 sizeof(ip_vs_master_mcast_ifn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 ip_vs_master_syncid = syncid;
906 } else {
Simon Horman37004af2006-12-10 22:36:18 -0800907 strlcpy(ip_vs_backup_mcast_ifn, mcast_ifn,
908 sizeof(ip_vs_backup_mcast_ifn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 ip_vs_backup_syncid = syncid;
910 }
911
Neil Hormancc0191a2007-06-18 22:33:20 -0700912 tinfo->state = state;
913 tinfo->startup = &startup;
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 repeat:
Neil Hormancc0191a2007-06-18 22:33:20 -0700916 if ((pid = kernel_thread(fork_sync_thread, tinfo, 0)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 IP_VS_ERR("could not create fork_sync_thread due to %d... "
918 "retrying.\n", pid);
Simon Horman89eaeb02006-12-10 22:35:24 -0800919 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 goto repeat;
921 }
922
923 wait_for_completion(&startup);
924
925 return 0;
926}
927
928
929int stop_sync_thread(int state)
930{
931 DECLARE_WAITQUEUE(wait, current);
932
933 if ((state == IP_VS_STATE_MASTER && !sync_master_pid) ||
934 (state == IP_VS_STATE_BACKUP && !sync_backup_pid))
935 return -ESRCH;
936
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700937 IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 IP_VS_INFO("stopping sync thread %d ...\n",
Simon Horman37004af2006-12-10 22:36:18 -0800939 (state == IP_VS_STATE_MASTER) ?
940 sync_master_pid : sync_backup_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 __set_current_state(TASK_UNINTERRUPTIBLE);
943 add_wait_queue(&stop_sync_wait, &wait);
944 set_stop_sync(state, 1);
945 ip_vs_sync_state -= state;
946 wake_up(&sync_wait);
947 schedule();
948 __set_current_state(TASK_RUNNING);
949 remove_wait_queue(&stop_sync_wait, &wait);
950
951 /* Note: no need to reap the sync thread, because its parent
952 process is the init process */
953
954 if ((state == IP_VS_STATE_MASTER && stop_master_sync) ||
955 (state == IP_VS_STATE_BACKUP && stop_backup_sync))
956 IP_VS_BUG();
957
958 return 0;
959}