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