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