VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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 = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
559         struct kvec     iov;
560         int             len;
561
562         EnterFunction(7);
563         iov.iov_base     = (void *)buffer;
564         iov.iov_len      = length;
565
566         len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
567
568         LeaveFunction(7);
569         return len;
570 }
571
572
573 static int
574 ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
575 {
576         struct msghdr           msg = {NULL,};
577         struct kvec             iov;
578         int                     len;
579
580         EnterFunction(7);
581
582         /* Receive a packet */
583         iov.iov_base     = buffer;
584         iov.iov_len      = (size_t)buflen;
585
586         len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, 0);
587
588         if (len < 0)
589                 return -1;
590
591         LeaveFunction(7);
592         return len;
593 }
594
595
596 static DECLARE_WAIT_QUEUE_HEAD(sync_wait);
597 static pid_t sync_master_pid = 0;
598 static pid_t sync_backup_pid = 0;
599
600 static DECLARE_WAIT_QUEUE_HEAD(stop_sync_wait);
601 static int stop_master_sync = 0;
602 static int stop_backup_sync = 0;
603
604 static void sync_master_loop(void)
605 {
606         struct socket *sock;
607         struct ip_vs_sync_buff *sb;
608         struct ip_vs_sync_mesg *m;
609
610         /* create the sending multicast socket */
611         sock = make_send_sock();
612         if (!sock)
613                 return;
614
615         IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, "
616                    "syncid = %d\n",
617                    ip_vs_master_mcast_ifn, ip_vs_master_syncid);
618
619         for (;;) {
620                 while ((sb=sb_dequeue())) {
621                         m = sb->mesg;
622                         if (ip_vs_send_async(sock, (char *)m,
623                                              m->size) != m->size)
624                                 IP_VS_ERR("ip_vs_send_async error\n");
625                         ip_vs_sync_buff_release(sb);
626                 }
627
628                 /* check if entries stay in curr_sb for 2 seconds */
629                 if ((sb = get_curr_sync_buff(2*HZ))) {
630                         m = sb->mesg;
631                         if (ip_vs_send_async(sock, (char *)m,
632                                              m->size) != m->size)
633                                 IP_VS_ERR("ip_vs_send_async error\n");
634                         ip_vs_sync_buff_release(sb);
635                 }
636
637                 if (stop_master_sync)
638                         break;
639
640                 __set_current_state(TASK_INTERRUPTIBLE);
641                 schedule_timeout(HZ);
642         }
643
644         /* clean up the sync_buff queue */
645         while ((sb=sb_dequeue())) {
646                 ip_vs_sync_buff_release(sb);
647         }
648
649         /* clean up the current sync_buff */
650         if ((sb = get_curr_sync_buff(0))) {
651                 ip_vs_sync_buff_release(sb);
652         }
653
654         /* release the sending multicast socket */
655         sock_release(sock);
656 }
657
658
659 static void sync_backup_loop(void)
660 {
661         struct socket *sock;
662         char *buf;
663         int len;
664
665         if (!(buf = kmalloc(sync_recv_mesg_maxlen, GFP_ATOMIC))) {
666                 IP_VS_ERR("sync_backup_loop: kmalloc error\n");
667                 return;
668         }
669
670         /* create the receiving multicast socket */
671         sock = make_receive_sock();
672         if (!sock)
673                 goto out;
674
675         IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
676                    "syncid = %d\n",
677                    ip_vs_backup_mcast_ifn, ip_vs_backup_syncid);
678
679         for (;;) {
680                 /* do you have data now? */
681                 while (!skb_queue_empty(&(sock->sk->sk_receive_queue))) {
682                         if ((len =
683                              ip_vs_receive(sock, buf,
684                                            sync_recv_mesg_maxlen)) <= 0) {
685                                 IP_VS_ERR("receiving message error\n");
686                                 break;
687                         }
688                         /* disable bottom half, because it accessed the data
689                            shared by softirq while getting/creating conns */
690                         local_bh_disable();
691                         ip_vs_process_message(buf, len);
692                         local_bh_enable();
693                 }
694
695                 if (stop_backup_sync)
696                         break;
697
698                 __set_current_state(TASK_INTERRUPTIBLE);
699                 schedule_timeout(HZ);
700         }
701
702         /* release the sending multicast socket */
703         sock_release(sock);
704
705   out:
706         kfree(buf);
707 }
708
709
710 static void set_sync_pid(int sync_state, pid_t sync_pid)
711 {
712         if (sync_state == IP_VS_STATE_MASTER)
713                 sync_master_pid = sync_pid;
714         else if (sync_state == IP_VS_STATE_BACKUP)
715                 sync_backup_pid = sync_pid;
716 }
717
718 static void set_stop_sync(int sync_state, int set)
719 {
720         if (sync_state == IP_VS_STATE_MASTER)
721                 stop_master_sync = set;
722         else if (sync_state == IP_VS_STATE_BACKUP)
723                 stop_backup_sync = set;
724         else {
725                 stop_master_sync = set;
726                 stop_backup_sync = set;
727         }
728 }
729
730 static int sync_thread(void *startup)
731 {
732         DECLARE_WAITQUEUE(wait, current);
733         mm_segment_t oldmm;
734         int state;
735         const char *name;
736
737         /* increase the module use count */
738         ip_vs_use_count_inc();
739
740         if (ip_vs_sync_state & IP_VS_STATE_MASTER && !sync_master_pid) {
741                 state = IP_VS_STATE_MASTER;
742                 name = "ipvs_syncmaster";
743         } else if (ip_vs_sync_state & IP_VS_STATE_BACKUP && !sync_backup_pid) {
744                 state = IP_VS_STATE_BACKUP;
745                 name = "ipvs_syncbackup";
746         } else {
747                 IP_VS_BUG();
748                 ip_vs_use_count_dec();
749                 return -EINVAL;
750         }
751
752         daemonize(name);
753
754         oldmm = get_fs();
755         set_fs(KERNEL_DS);
756
757         /* Block all signals */
758         spin_lock_irq(&current->sighand->siglock);
759         siginitsetinv(&current->blocked, 0);
760         recalc_sigpending();
761         spin_unlock_irq(&current->sighand->siglock);
762
763         /* set the maximum length of sync message */
764         set_sync_mesg_maxlen(state);
765
766         /* set up multicast address */
767         mcast_addr.sin_family = AF_INET;
768         mcast_addr.sin_port = htons(IP_VS_SYNC_PORT);
769         mcast_addr.sin_addr.s_addr = htonl(IP_VS_SYNC_GROUP);
770
771         add_wait_queue(&sync_wait, &wait);
772
773         set_sync_pid(state, current->pid);
774         complete((struct completion *)startup);
775
776         /* processing master/backup loop here */
777         if (state == IP_VS_STATE_MASTER)
778                 sync_master_loop();
779         else if (state == IP_VS_STATE_BACKUP)
780                 sync_backup_loop();
781         else IP_VS_BUG();
782
783         remove_wait_queue(&sync_wait, &wait);
784
785         /* thread exits */
786         set_sync_pid(state, 0);
787         IP_VS_INFO("sync thread stopped!\n");
788
789         set_fs(oldmm);
790
791         /* decrease the module use count */
792         ip_vs_use_count_dec();
793
794         set_stop_sync(state, 0);
795         wake_up(&stop_sync_wait);
796
797         return 0;
798 }
799
800
801 static int fork_sync_thread(void *startup)
802 {
803         pid_t pid;
804
805         /* fork the sync thread here, then the parent process of the
806            sync thread is the init process after this thread exits. */
807   repeat:
808         if ((pid = kernel_thread(sync_thread, startup, 0)) < 0) {
809                 IP_VS_ERR("could not create sync_thread due to %d... "
810                           "retrying.\n", pid);
811                 current->state = TASK_UNINTERRUPTIBLE;
812                 schedule_timeout(HZ);
813                 goto repeat;
814         }
815
816         return 0;
817 }
818
819
820 int start_sync_thread(int state, char *mcast_ifn, __u8 syncid)
821 {
822         DECLARE_COMPLETION(startup);
823         pid_t pid;
824
825         if ((state == IP_VS_STATE_MASTER && sync_master_pid) ||
826             (state == IP_VS_STATE_BACKUP && sync_backup_pid))
827                 return -EEXIST;
828
829         IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, current->pid);
830         IP_VS_DBG(7, "Each ip_vs_sync_conn entry need %Zd bytes\n",
831                   sizeof(struct ip_vs_sync_conn));
832
833         ip_vs_sync_state |= state;
834         if (state == IP_VS_STATE_MASTER) {
835                 strcpy(ip_vs_master_mcast_ifn, mcast_ifn);
836                 ip_vs_master_syncid = syncid;
837         } else {
838                 strcpy(ip_vs_backup_mcast_ifn, mcast_ifn);
839                 ip_vs_backup_syncid = syncid;
840         }
841
842   repeat:
843         if ((pid = kernel_thread(fork_sync_thread, &startup, 0)) < 0) {
844                 IP_VS_ERR("could not create fork_sync_thread due to %d... "
845                           "retrying.\n", pid);
846                 current->state = TASK_UNINTERRUPTIBLE;
847                 schedule_timeout(HZ);
848                 goto repeat;
849         }
850
851         wait_for_completion(&startup);
852
853         return 0;
854 }
855
856
857 int stop_sync_thread(int state)
858 {
859         DECLARE_WAITQUEUE(wait, current);
860
861         if ((state == IP_VS_STATE_MASTER && !sync_master_pid) ||
862             (state == IP_VS_STATE_BACKUP && !sync_backup_pid))
863                 return -ESRCH;
864
865         IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, current->pid);
866         IP_VS_INFO("stopping sync thread %d ...\n",
867                    (state == IP_VS_STATE_MASTER) ? sync_master_pid : sync_backup_pid);
868
869         __set_current_state(TASK_UNINTERRUPTIBLE);
870         add_wait_queue(&stop_sync_wait, &wait);
871         set_stop_sync(state, 1);
872         ip_vs_sync_state -= state;
873         wake_up(&sync_wait);
874         schedule();
875         __set_current_state(TASK_RUNNING);
876         remove_wait_queue(&stop_sync_wait, &wait);
877
878         /* Note: no need to reap the sync thread, because its parent
879            process is the init process */
880
881         if ((state == IP_VS_STATE_MASTER && stop_master_sync) ||
882             (state == IP_VS_STATE_BACKUP && stop_backup_sync))
883                 IP_VS_BUG();
884
885         return 0;
886 }