Merge ../linux-2.6/
[linux-2.6] / net / ipv4 / ipvs / ip_vs_sync.c
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  *
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>
24 #include <linux/net.h>
25 #include <linux/completion.h>
26 #include <linux/delay.h>
27 #include <linux/skbuff.h>
28 #include <linux/in.h>
29 #include <linux/igmp.h>                 /* for ip_mc_join_group */
30
31 #include <net/ip.h>
32 #include <net/sock.h>
33 #include <asm/uaccess.h>                /* for get_fs and set_fs */
34
35 #include <net/ip_vs.h>
36
37 #define IP_VS_SYNC_GROUP 0xe0000051    /* multicast addr - 224.0.0.81 */
38 #define IP_VS_SYNC_PORT  8848          /* multicast port */
39
40
41 /*
42  *      IPVS sync connection entry
43  */
44 struct ip_vs_sync_conn {
45         __u8                    reserved;
46
47         /* Protocol, addresses and port numbers */
48         __u8                    protocol;       /* Which protocol (TCP/UDP) */
49         __u16                   cport;
50         __u16                   vport;
51         __u16                   dport;
52         __u32                   caddr;          /* client address */
53         __u32                   vaddr;          /* virtual address */
54         __u32                   daddr;          /* destination address */
55
56         /* Flags and state transition */
57         __u16                   flags;          /* status flags */
58         __u16                   state;          /* state info */
59
60         /* The sequence options start here */
61 };
62
63 struct ip_vs_sync_conn_options {
64         struct ip_vs_seq        in_seq;         /* incoming seq. struct */
65         struct ip_vs_seq        out_seq;        /* outgoing seq. struct */
66 };
67
68 #define IP_VS_SYNC_CONN_TIMEOUT (3*60*HZ)
69 #define SIMPLE_CONN_SIZE  (sizeof(struct ip_vs_sync_conn))
70 #define FULL_CONN_SIZE  \
71 (sizeof(struct ip_vs_sync_conn) + sizeof(struct ip_vs_sync_conn_options))
72
73
74 /*
75   The master mulitcasts messages to the backup load balancers in the
76   following format.
77
78        0                   1                   2                   3
79        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
80       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
81       |  Count Conns  |    SyncID     |            Size               |
82       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
83       |                                                               |
84       |                    IPVS Sync Connection (1)                   |
85       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
86       |                            .                                  |
87       |                            .                                  |
88       |                            .                                  |
89       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90       |                                                               |
91       |                    IPVS Sync Connection (n)                   |
92       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93 */
94
95 #define SYNC_MESG_HEADER_LEN    4
96
97 struct ip_vs_sync_mesg {
98         __u8                    nr_conns;
99         __u8                    syncid;
100         __u16                   size;
101
102         /* ip_vs_sync_conn entries start here */
103 };
104
105 /* the maximum length of sync (sending/receiving) message */
106 static int sync_send_mesg_maxlen;
107 static int sync_recv_mesg_maxlen;
108
109 struct ip_vs_sync_buff {
110         struct list_head        list;
111         unsigned long           firstuse;
112
113         /* pointers for the message data */
114         struct ip_vs_sync_mesg  *mesg;
115         unsigned char           *head;
116         unsigned char           *end;
117 };
118
119
120 /* the sync_buff list head and the lock */
121 static LIST_HEAD(ip_vs_sync_queue);
122 static DEFINE_SPINLOCK(ip_vs_sync_lock);
123
124 /* current sync_buff for accepting new conn entries */
125 static struct ip_vs_sync_buff   *curr_sb = NULL;
126 static DEFINE_SPINLOCK(curr_sb_lock);
127
128 /* ipvs sync daemon state */
129 volatile int ip_vs_sync_state = IP_VS_STATE_NONE;
130 volatile int ip_vs_master_syncid = 0;
131 volatile int ip_vs_backup_syncid = 0;
132
133 /* multicast interface name */
134 char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
135 char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
136
137 /* multicast addr */
138 static struct sockaddr_in mcast_addr;
139
140
141 static inline void sb_queue_tail(struct ip_vs_sync_buff *sb)
142 {
143         spin_lock(&ip_vs_sync_lock);
144         list_add_tail(&sb->list, &ip_vs_sync_queue);
145         spin_unlock(&ip_vs_sync_lock);
146 }
147
148 static inline struct ip_vs_sync_buff * sb_dequeue(void)
149 {
150         struct ip_vs_sync_buff *sb;
151
152         spin_lock_bh(&ip_vs_sync_lock);
153         if (list_empty(&ip_vs_sync_queue)) {
154                 sb = NULL;
155         } else {
156                 sb = list_entry(ip_vs_sync_queue.next,
157                                 struct ip_vs_sync_buff,
158                                 list);
159                 list_del(&sb->list);
160         }
161         spin_unlock_bh(&ip_vs_sync_lock);
162
163         return sb;
164 }
165
166 static inline struct ip_vs_sync_buff * ip_vs_sync_buff_create(void)
167 {
168         struct ip_vs_sync_buff *sb;
169
170         if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
171                 return NULL;
172
173         if (!(sb->mesg=kmalloc(sync_send_mesg_maxlen, GFP_ATOMIC))) {
174                 kfree(sb);
175                 return NULL;
176         }
177         sb->mesg->nr_conns = 0;
178         sb->mesg->syncid = ip_vs_master_syncid;
179         sb->mesg->size = 4;
180         sb->head = (unsigned char *)sb->mesg + 4;
181         sb->end = (unsigned char *)sb->mesg + sync_send_mesg_maxlen;
182         sb->firstuse = jiffies;
183         return sb;
184 }
185
186 static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff *sb)
187 {
188         kfree(sb->mesg);
189         kfree(sb);
190 }
191
192 /*
193  *      Get the current sync buffer if it has been created for more
194  *      than the specified time or the specified time is zero.
195  */
196 static inline struct ip_vs_sync_buff *
197 get_curr_sync_buff(unsigned long time)
198 {
199         struct ip_vs_sync_buff *sb;
200
201         spin_lock_bh(&curr_sb_lock);
202         if (curr_sb && (time == 0 ||
203                         time_before(jiffies - curr_sb->firstuse, time))) {
204                 sb = curr_sb;
205                 curr_sb = NULL;
206         } else
207                 sb = NULL;
208         spin_unlock_bh(&curr_sb_lock);
209         return sb;
210 }
211
212
213 /*
214  *      Add an ip_vs_conn information into the current sync_buff.
215  *      Called by ip_vs_in.
216  */
217 void ip_vs_sync_conn(struct ip_vs_conn *cp)
218 {
219         struct ip_vs_sync_mesg *m;
220         struct ip_vs_sync_conn *s;
221         int len;
222
223         spin_lock(&curr_sb_lock);
224         if (!curr_sb) {
225                 if (!(curr_sb=ip_vs_sync_buff_create())) {
226                         spin_unlock(&curr_sb_lock);
227                         IP_VS_ERR("ip_vs_sync_buff_create failed.\n");
228                         return;
229                 }
230         }
231
232         len = (cp->flags & IP_VS_CONN_F_SEQ_MASK) ? FULL_CONN_SIZE :
233                 SIMPLE_CONN_SIZE;
234         m = curr_sb->mesg;
235         s = (struct ip_vs_sync_conn *)curr_sb->head;
236
237         /* copy members */
238         s->protocol = cp->protocol;
239         s->cport = cp->cport;
240         s->vport = cp->vport;
241         s->dport = cp->dport;
242         s->caddr = cp->caddr;
243         s->vaddr = cp->vaddr;
244         s->daddr = cp->daddr;
245         s->flags = htons(cp->flags & ~IP_VS_CONN_F_HASHED);
246         s->state = htons(cp->state);
247         if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
248                 struct ip_vs_sync_conn_options *opt =
249                         (struct ip_vs_sync_conn_options *)&s[1];
250                 memcpy(opt, &cp->in_seq, sizeof(*opt));
251         }
252
253         m->nr_conns++;
254         m->size += len;
255         curr_sb->head += len;
256
257         /* check if there is a space for next one */
258         if (curr_sb->head+FULL_CONN_SIZE > curr_sb->end) {
259                 sb_queue_tail(curr_sb);
260                 curr_sb = NULL;
261         }
262         spin_unlock(&curr_sb_lock);
263
264         /* synchronize its controller if it has */
265         if (cp->control)
266                 ip_vs_sync_conn(cp->control);
267 }
268
269
270 /*
271  *      Process received multicast message and create the corresponding
272  *      ip_vs_conn entries.
273  */
274 static void ip_vs_process_message(const char *buffer, const size_t buflen)
275 {
276         struct ip_vs_sync_mesg *m = (struct ip_vs_sync_mesg *)buffer;
277         struct ip_vs_sync_conn *s;
278         struct ip_vs_sync_conn_options *opt;
279         struct ip_vs_conn *cp;
280         char *p;
281         int i;
282
283         /* Convert size back to host byte order */
284         m->size = ntohs(m->size);
285
286         if (buflen != m->size) {
287                 IP_VS_ERR("bogus message\n");
288                 return;
289         }
290
291         /* SyncID sanity check */
292         if (ip_vs_backup_syncid != 0 && m->syncid != ip_vs_backup_syncid) {
293                 IP_VS_DBG(7, "Ignoring incoming msg with syncid = %d\n",
294                           m->syncid);
295                 return;
296         }
297
298         p = (char *)buffer + sizeof(struct ip_vs_sync_mesg);
299         for (i=0; i<m->nr_conns; i++) {
300                 s = (struct ip_vs_sync_conn *)p;
301                 cp = ip_vs_conn_in_get(s->protocol,
302                                        s->caddr, s->cport,
303                                        s->vaddr, s->vport);
304                 if (!cp) {
305                         cp = ip_vs_conn_new(s->protocol,
306                                             s->caddr, s->cport,
307                                             s->vaddr, s->vport,
308                                             s->daddr, s->dport,
309                                             ntohs(s->flags), NULL);
310                         if (!cp) {
311                                 IP_VS_ERR("ip_vs_conn_new failed\n");
312                                 return;
313                         }
314                         cp->state = ntohs(s->state);
315                 } else if (!cp->dest) {
316                         /* it is an entry created by the synchronization */
317                         cp->state = ntohs(s->state);
318                         cp->flags = ntohs(s->flags) | IP_VS_CONN_F_HASHED;
319                 }       /* Note that we don't touch its state and flags
320                            if it is a normal entry. */
321
322                 if (ntohs(s->flags) & IP_VS_CONN_F_SEQ_MASK) {
323                         opt = (struct ip_vs_sync_conn_options *)&s[1];
324                         memcpy(&cp->in_seq, opt, sizeof(*opt));
325                         p += FULL_CONN_SIZE;
326                 } else
327                         p += SIMPLE_CONN_SIZE;
328
329                 atomic_set(&cp->in_pkts, sysctl_ip_vs_sync_threshold[0]);
330                 cp->timeout = IP_VS_SYNC_CONN_TIMEOUT;
331                 ip_vs_conn_put(cp);
332
333                 if (p > buffer+buflen) {
334                         IP_VS_ERR("bogus message\n");
335                         return;
336                 }
337         }
338 }
339
340
341 /*
342  *      Setup loopback of outgoing multicasts on a sending socket
343  */
344 static void set_mcast_loop(struct sock *sk, u_char loop)
345 {
346         struct inet_sock *inet = inet_sk(sk);
347
348         /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
349         lock_sock(sk);
350         inet->mc_loop = loop ? 1 : 0;
351         release_sock(sk);
352 }
353
354 /*
355  *      Specify TTL for outgoing multicasts on a sending socket
356  */
357 static void set_mcast_ttl(struct sock *sk, u_char ttl)
358 {
359         struct inet_sock *inet = inet_sk(sk);
360
361         /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
362         lock_sock(sk);
363         inet->mc_ttl = ttl;
364         release_sock(sk);
365 }
366
367 /*
368  *      Specifiy default interface for outgoing multicasts
369  */
370 static int set_mcast_if(struct sock *sk, char *ifname)
371 {
372         struct net_device *dev;
373         struct inet_sock *inet = inet_sk(sk);
374
375         if ((dev = __dev_get_by_name(ifname)) == NULL)
376                 return -ENODEV;
377
378         if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
379                 return -EINVAL;
380
381         lock_sock(sk);
382         inet->mc_index = dev->ifindex;
383         /*  inet->mc_addr  = 0; */
384         release_sock(sk);
385
386         return 0;
387 }
388
389
390 /*
391  *      Set the maximum length of sync message according to the
392  *      specified interface's MTU.
393  */
394 static int set_sync_mesg_maxlen(int sync_state)
395 {
396         struct net_device *dev;
397         int num;
398
399         if (sync_state == IP_VS_STATE_MASTER) {
400                 if ((dev = __dev_get_by_name(ip_vs_master_mcast_ifn)) == NULL)
401                         return -ENODEV;
402
403                 num = (dev->mtu - sizeof(struct iphdr) -
404                        sizeof(struct udphdr) -
405                        SYNC_MESG_HEADER_LEN - 20) / SIMPLE_CONN_SIZE;
406                 sync_send_mesg_maxlen =
407                         SYNC_MESG_HEADER_LEN + SIMPLE_CONN_SIZE * num;
408                 IP_VS_DBG(7, "setting the maximum length of sync sending "
409                           "message %d.\n", sync_send_mesg_maxlen);
410         } else if (sync_state == IP_VS_STATE_BACKUP) {
411                 if ((dev = __dev_get_by_name(ip_vs_backup_mcast_ifn)) == NULL)
412                         return -ENODEV;
413
414                 sync_recv_mesg_maxlen = dev->mtu -
415                         sizeof(struct iphdr) - sizeof(struct udphdr);
416                 IP_VS_DBG(7, "setting the maximum length of sync receiving "
417                           "message %d.\n", sync_recv_mesg_maxlen);
418         }
419
420         return 0;
421 }
422
423
424 /*
425  *      Join a multicast group.
426  *      the group is specified by a class D multicast address 224.0.0.0/8
427  *      in the in_addr structure passed in as a parameter.
428  */
429 static int
430 join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
431 {
432         struct ip_mreqn mreq;
433         struct net_device *dev;
434         int ret;
435
436         memset(&mreq, 0, sizeof(mreq));
437         memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
438
439         if ((dev = __dev_get_by_name(ifname)) == NULL)
440                 return -ENODEV;
441         if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
442                 return -EINVAL;
443
444         mreq.imr_ifindex = dev->ifindex;
445
446         lock_sock(sk);
447         ret = ip_mc_join_group(sk, &mreq);
448         release_sock(sk);
449
450         return ret;
451 }
452
453
454 static int bind_mcastif_addr(struct socket *sock, char *ifname)
455 {
456         struct net_device *dev;
457         u32 addr;
458         struct sockaddr_in sin;
459
460         if ((dev = __dev_get_by_name(ifname)) == NULL)
461                 return -ENODEV;
462
463         addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
464         if (!addr)
465                 IP_VS_ERR("You probably need to specify IP address on "
466                           "multicast interface.\n");
467
468         IP_VS_DBG(7, "binding socket with (%s) %u.%u.%u.%u\n",
469                   ifname, NIPQUAD(addr));
470
471         /* Now bind the socket with the address of multicast interface */
472         sin.sin_family       = AF_INET;
473         sin.sin_addr.s_addr  = addr;
474         sin.sin_port         = 0;
475
476         return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
477 }
478
479 /*
480  *      Set up sending multicast socket over UDP
481  */
482 static struct socket * make_send_sock(void)
483 {
484         struct socket *sock;
485
486         /* First create a socket */
487         if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
488                 IP_VS_ERR("Error during creation of socket; terminating\n");
489                 return NULL;
490         }
491
492         if (set_mcast_if(sock->sk, ip_vs_master_mcast_ifn) < 0) {
493                 IP_VS_ERR("Error setting outbound mcast interface\n");
494                 goto error;
495         }
496
497         set_mcast_loop(sock->sk, 0);
498         set_mcast_ttl(sock->sk, 1);
499
500         if (bind_mcastif_addr(sock, ip_vs_master_mcast_ifn) < 0) {
501                 IP_VS_ERR("Error binding address of the mcast interface\n");
502                 goto error;
503         }
504
505         if (sock->ops->connect(sock,
506                                (struct sockaddr*)&mcast_addr,
507                                sizeof(struct sockaddr), 0) < 0) {
508                 IP_VS_ERR("Error connecting to the multicast addr\n");
509                 goto error;
510         }
511
512         return sock;
513
514   error:
515         sock_release(sock);
516         return NULL;
517 }
518
519
520 /*
521  *      Set up receiving multicast socket over UDP
522  */
523 static struct socket * make_receive_sock(void)
524 {
525         struct socket *sock;
526
527         /* First create a socket */
528         if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
529                 IP_VS_ERR("Error during creation of socket; terminating\n");
530                 return NULL;
531         }
532
533         /* it is equivalent to the REUSEADDR option in user-space */
534         sock->sk->sk_reuse = 1;
535
536         if (sock->ops->bind(sock,
537                             (struct sockaddr*)&mcast_addr,
538                             sizeof(struct sockaddr)) < 0) {
539                 IP_VS_ERR("Error binding to the multicast addr\n");
540                 goto error;
541         }
542
543         /* join the multicast group */
544         if (join_mcast_group(sock->sk,
545                              (struct in_addr*)&mcast_addr.sin_addr,
546                              ip_vs_backup_mcast_ifn) < 0) {
547                 IP_VS_ERR("Error joining to the multicast group\n");
548                 goto error;
549         }
550
551         return sock;
552
553   error:
554         sock_release(sock);
555         return NULL;
556 }
557
558
559 static int
560 ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
561 {
562         struct msghdr   msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
563         struct kvec     iov;
564         int             len;
565
566         EnterFunction(7);
567         iov.iov_base     = (void *)buffer;
568         iov.iov_len      = length;
569
570         len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
571
572         LeaveFunction(7);
573         return len;
574 }
575
576 static void
577 ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
578 {
579         int msize;
580
581         msize = msg->size;
582
583         /* Put size in network byte order */
584         msg->size = htons(msg->size);
585
586         if (ip_vs_send_async(sock, (char *)msg, msize) != msize)
587                 IP_VS_ERR("ip_vs_send_async error\n");
588 }
589
590 static int
591 ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
592 {
593         struct msghdr           msg = {NULL,};
594         struct kvec             iov;
595         int                     len;
596
597         EnterFunction(7);
598
599         /* Receive a packet */
600         iov.iov_base     = buffer;
601         iov.iov_len      = (size_t)buflen;
602
603         len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, 0);
604
605         if (len < 0)
606                 return -1;
607
608         LeaveFunction(7);
609         return len;
610 }
611
612
613 static DECLARE_WAIT_QUEUE_HEAD(sync_wait);
614 static pid_t sync_master_pid = 0;
615 static pid_t sync_backup_pid = 0;
616
617 static DECLARE_WAIT_QUEUE_HEAD(stop_sync_wait);
618 static int stop_master_sync = 0;
619 static int stop_backup_sync = 0;
620
621 static void sync_master_loop(void)
622 {
623         struct socket *sock;
624         struct ip_vs_sync_buff *sb;
625
626         /* create the sending multicast socket */
627         sock = make_send_sock();
628         if (!sock)
629                 return;
630
631         IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, "
632                    "syncid = %d\n",
633                    ip_vs_master_mcast_ifn, ip_vs_master_syncid);
634
635         for (;;) {
636                 while ((sb=sb_dequeue())) {
637                         ip_vs_send_sync_msg(sock, sb->mesg);
638                         ip_vs_sync_buff_release(sb);
639                 }
640
641                 /* check if entries stay in curr_sb for 2 seconds */
642                 if ((sb = get_curr_sync_buff(2*HZ))) {
643                         ip_vs_send_sync_msg(sock, sb->mesg);
644                         ip_vs_sync_buff_release(sb);
645                 }
646
647                 if (stop_master_sync)
648                         break;
649
650                 ssleep(1);
651         }
652
653         /* clean up the sync_buff queue */
654         while ((sb=sb_dequeue())) {
655                 ip_vs_sync_buff_release(sb);
656         }
657
658         /* clean up the current sync_buff */
659         if ((sb = get_curr_sync_buff(0))) {
660                 ip_vs_sync_buff_release(sb);
661         }
662
663         /* release the sending multicast socket */
664         sock_release(sock);
665 }
666
667
668 static void sync_backup_loop(void)
669 {
670         struct socket *sock;
671         char *buf;
672         int len;
673
674         if (!(buf = kmalloc(sync_recv_mesg_maxlen, GFP_ATOMIC))) {
675                 IP_VS_ERR("sync_backup_loop: kmalloc error\n");
676                 return;
677         }
678
679         /* create the receiving multicast socket */
680         sock = make_receive_sock();
681         if (!sock)
682                 goto out;
683
684         IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
685                    "syncid = %d\n",
686                    ip_vs_backup_mcast_ifn, ip_vs_backup_syncid);
687
688         for (;;) {
689                 /* do you have data now? */
690                 while (!skb_queue_empty(&(sock->sk->sk_receive_queue))) {
691                         if ((len =
692                              ip_vs_receive(sock, buf,
693                                            sync_recv_mesg_maxlen)) <= 0) {
694                                 IP_VS_ERR("receiving message error\n");
695                                 break;
696                         }
697                         /* disable bottom half, because it accessed the data
698                            shared by softirq while getting/creating conns */
699                         local_bh_disable();
700                         ip_vs_process_message(buf, len);
701                         local_bh_enable();
702                 }
703
704                 if (stop_backup_sync)
705                         break;
706
707                 ssleep(1);
708         }
709
710         /* release the sending multicast socket */
711         sock_release(sock);
712
713   out:
714         kfree(buf);
715 }
716
717
718 static void set_sync_pid(int sync_state, pid_t sync_pid)
719 {
720         if (sync_state == IP_VS_STATE_MASTER)
721                 sync_master_pid = sync_pid;
722         else if (sync_state == IP_VS_STATE_BACKUP)
723                 sync_backup_pid = sync_pid;
724 }
725
726 static void set_stop_sync(int sync_state, int set)
727 {
728         if (sync_state == IP_VS_STATE_MASTER)
729                 stop_master_sync = set;
730         else if (sync_state == IP_VS_STATE_BACKUP)
731                 stop_backup_sync = set;
732         else {
733                 stop_master_sync = set;
734                 stop_backup_sync = set;
735         }
736 }
737
738 static int sync_thread(void *startup)
739 {
740         DECLARE_WAITQUEUE(wait, current);
741         mm_segment_t oldmm;
742         int state;
743         const char *name;
744
745         /* increase the module use count */
746         ip_vs_use_count_inc();
747
748         if (ip_vs_sync_state & IP_VS_STATE_MASTER && !sync_master_pid) {
749                 state = IP_VS_STATE_MASTER;
750                 name = "ipvs_syncmaster";
751         } else if (ip_vs_sync_state & IP_VS_STATE_BACKUP && !sync_backup_pid) {
752                 state = IP_VS_STATE_BACKUP;
753                 name = "ipvs_syncbackup";
754         } else {
755                 IP_VS_BUG();
756                 ip_vs_use_count_dec();
757                 return -EINVAL;
758         }
759
760         daemonize(name);
761
762         oldmm = get_fs();
763         set_fs(KERNEL_DS);
764
765         /* Block all signals */
766         spin_lock_irq(&current->sighand->siglock);
767         siginitsetinv(&current->blocked, 0);
768         recalc_sigpending();
769         spin_unlock_irq(&current->sighand->siglock);
770
771         /* set the maximum length of sync message */
772         set_sync_mesg_maxlen(state);
773
774         /* set up multicast address */
775         mcast_addr.sin_family = AF_INET;
776         mcast_addr.sin_port = htons(IP_VS_SYNC_PORT);
777         mcast_addr.sin_addr.s_addr = htonl(IP_VS_SYNC_GROUP);
778
779         add_wait_queue(&sync_wait, &wait);
780
781         set_sync_pid(state, current->pid);
782         complete((struct completion *)startup);
783
784         /* processing master/backup loop here */
785         if (state == IP_VS_STATE_MASTER)
786                 sync_master_loop();
787         else if (state == IP_VS_STATE_BACKUP)
788                 sync_backup_loop();
789         else IP_VS_BUG();
790
791         remove_wait_queue(&sync_wait, &wait);
792
793         /* thread exits */
794         set_sync_pid(state, 0);
795         IP_VS_INFO("sync thread stopped!\n");
796
797         set_fs(oldmm);
798
799         /* decrease the module use count */
800         ip_vs_use_count_dec();
801
802         set_stop_sync(state, 0);
803         wake_up(&stop_sync_wait);
804
805         return 0;
806 }
807
808
809 static int fork_sync_thread(void *startup)
810 {
811         pid_t pid;
812
813         /* fork the sync thread here, then the parent process of the
814            sync thread is the init process after this thread exits. */
815   repeat:
816         if ((pid = kernel_thread(sync_thread, startup, 0)) < 0) {
817                 IP_VS_ERR("could not create sync_thread due to %d... "
818                           "retrying.\n", pid);
819                 ssleep(1);
820                 goto repeat;
821         }
822
823         return 0;
824 }
825
826
827 int start_sync_thread(int state, char *mcast_ifn, __u8 syncid)
828 {
829         DECLARE_COMPLETION(startup);
830         pid_t pid;
831
832         if ((state == IP_VS_STATE_MASTER && sync_master_pid) ||
833             (state == IP_VS_STATE_BACKUP && sync_backup_pid))
834                 return -EEXIST;
835
836         IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, current->pid);
837         IP_VS_DBG(7, "Each ip_vs_sync_conn entry need %Zd bytes\n",
838                   sizeof(struct ip_vs_sync_conn));
839
840         ip_vs_sync_state |= state;
841         if (state == IP_VS_STATE_MASTER) {
842                 strlcpy(ip_vs_master_mcast_ifn, mcast_ifn, sizeof(ip_vs_master_mcast_ifn));
843                 ip_vs_master_syncid = syncid;
844         } else {
845                 strlcpy(ip_vs_backup_mcast_ifn, mcast_ifn, sizeof(ip_vs_backup_mcast_ifn));
846                 ip_vs_backup_syncid = syncid;
847         }
848
849   repeat:
850         if ((pid = kernel_thread(fork_sync_thread, &startup, 0)) < 0) {
851                 IP_VS_ERR("could not create fork_sync_thread due to %d... "
852                           "retrying.\n", pid);
853                 ssleep(1);
854                 goto repeat;
855         }
856
857         wait_for_completion(&startup);
858
859         return 0;
860 }
861
862
863 int stop_sync_thread(int state)
864 {
865         DECLARE_WAITQUEUE(wait, current);
866
867         if ((state == IP_VS_STATE_MASTER && !sync_master_pid) ||
868             (state == IP_VS_STATE_BACKUP && !sync_backup_pid))
869                 return -ESRCH;
870
871         IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, current->pid);
872         IP_VS_INFO("stopping sync thread %d ...\n",
873                    (state == IP_VS_STATE_MASTER) ? sync_master_pid : sync_backup_pid);
874
875         __set_current_state(TASK_UNINTERRUPTIBLE);
876         add_wait_queue(&stop_sync_wait, &wait);
877         set_stop_sync(state, 1);
878         ip_vs_sync_state -= state;
879         wake_up(&sync_wait);
880         schedule();
881         __set_current_state(TASK_RUNNING);
882         remove_wait_queue(&stop_sync_wait, &wait);
883
884         /* Note: no need to reap the sync thread, because its parent
885            process is the init process */
886
887         if ((state == IP_VS_STATE_MASTER && stop_master_sync) ||
888             (state == IP_VS_STATE_BACKUP && stop_backup_sync))
889                 IP_VS_BUG();
890
891         return 0;
892 }