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