[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / net / ipv4 / ipvs / ip_vs_sync.c
blob25c479550a32f32c64223fcd3fae8f827fc8f017
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.
8 * Version: $Id: ip_vs_sync.c,v 1.13 2003/06/08 09:31:19 wensong Exp $
10 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
12 * ip_vs_sync: sync connection info from master load balancer to backups
13 * through multicast
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.
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/net.h>
25 #include <linux/completion.h>
26 #include <linux/delay.h>
27 #include <linux/skbuff.h>
28 #include <linux/in.h>
29 #include <linux/igmp.h> /* for ip_mc_join_group */
31 #include <net/ip.h>
32 #include <net/sock.h>
33 #include <asm/uaccess.h> /* for get_fs and set_fs */
35 #include <net/ip_vs.h>
37 #define IP_VS_SYNC_GROUP 0xe0000051 /* multicast addr - 224.0.0.81 */
38 #define IP_VS_SYNC_PORT 8848 /* multicast port */
42 * IPVS sync connection entry
44 struct ip_vs_sync_conn {
45 __u8 reserved;
47 /* Protocol, addresses and port numbers */
48 __u8 protocol; /* Which protocol (TCP/UDP) */
49 __u16 cport;
50 __u16 vport;
51 __u16 dport;
52 __u32 caddr; /* client address */
53 __u32 vaddr; /* virtual address */
54 __u32 daddr; /* destination address */
56 /* Flags and state transition */
57 __u16 flags; /* status flags */
58 __u16 state; /* state info */
60 /* The sequence options start here */
63 struct ip_vs_sync_conn_options {
64 struct ip_vs_seq in_seq; /* incoming seq. struct */
65 struct ip_vs_seq out_seq; /* outgoing seq. struct */
68 #define IP_VS_SYNC_CONN_TIMEOUT (3*60*HZ)
69 #define SIMPLE_CONN_SIZE (sizeof(struct ip_vs_sync_conn))
70 #define FULL_CONN_SIZE \
71 (sizeof(struct ip_vs_sync_conn) + sizeof(struct ip_vs_sync_conn_options))
75 The master mulitcasts messages to the backup load balancers in the
76 following format.
78 0 1 2 3
79 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
80 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
81 | Count Conns | SyncID | Size |
82 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
83 | |
84 | IPVS Sync Connection (1) |
85 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
86 | . |
87 | . |
88 | . |
89 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90 | |
91 | IPVS Sync Connection (n) |
92 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95 #define SYNC_MESG_HEADER_LEN 4
97 struct ip_vs_sync_mesg {
98 __u8 nr_conns;
99 __u8 syncid;
100 __u16 size;
102 /* ip_vs_sync_conn entries start here */
105 /* the maximum length of sync (sending/receiving) message */
106 static int sync_send_mesg_maxlen;
107 static int sync_recv_mesg_maxlen;
109 struct ip_vs_sync_buff {
110 struct list_head list;
111 unsigned long firstuse;
113 /* pointers for the message data */
114 struct ip_vs_sync_mesg *mesg;
115 unsigned char *head;
116 unsigned char *end;
120 /* the sync_buff list head and the lock */
121 static LIST_HEAD(ip_vs_sync_queue);
122 static DEFINE_SPINLOCK(ip_vs_sync_lock);
124 /* current sync_buff for accepting new conn entries */
125 static struct ip_vs_sync_buff *curr_sb = NULL;
126 static DEFINE_SPINLOCK(curr_sb_lock);
128 /* ipvs sync daemon state */
129 volatile int ip_vs_sync_state = IP_VS_STATE_NONE;
130 volatile int ip_vs_master_syncid = 0;
131 volatile int ip_vs_backup_syncid = 0;
133 /* multicast interface name */
134 char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
135 char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
137 /* multicast addr */
138 static struct sockaddr_in mcast_addr;
141 static inline void sb_queue_tail(struct ip_vs_sync_buff *sb)
143 spin_lock(&ip_vs_sync_lock);
144 list_add_tail(&sb->list, &ip_vs_sync_queue);
145 spin_unlock(&ip_vs_sync_lock);
148 static inline struct ip_vs_sync_buff * sb_dequeue(void)
150 struct ip_vs_sync_buff *sb;
152 spin_lock_bh(&ip_vs_sync_lock);
153 if (list_empty(&ip_vs_sync_queue)) {
154 sb = NULL;
155 } else {
156 sb = list_entry(ip_vs_sync_queue.next,
157 struct ip_vs_sync_buff,
158 list);
159 list_del(&sb->list);
161 spin_unlock_bh(&ip_vs_sync_lock);
163 return sb;
166 static inline struct ip_vs_sync_buff * ip_vs_sync_buff_create(void)
168 struct ip_vs_sync_buff *sb;
170 if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
171 return NULL;
173 if (!(sb->mesg=kmalloc(sync_send_mesg_maxlen, GFP_ATOMIC))) {
174 kfree(sb);
175 return NULL;
177 sb->mesg->nr_conns = 0;
178 sb->mesg->syncid = ip_vs_master_syncid;
179 sb->mesg->size = 4;
180 sb->head = (unsigned char *)sb->mesg + 4;
181 sb->end = (unsigned char *)sb->mesg + sync_send_mesg_maxlen;
182 sb->firstuse = jiffies;
183 return sb;
186 static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff *sb)
188 kfree(sb->mesg);
189 kfree(sb);
193 * Get the current sync buffer if it has been created for more
194 * than the specified time or the specified time is zero.
196 static inline struct ip_vs_sync_buff *
197 get_curr_sync_buff(unsigned long time)
199 struct ip_vs_sync_buff *sb;
201 spin_lock_bh(&curr_sb_lock);
202 if (curr_sb && (time == 0 ||
203 time_before(jiffies - curr_sb->firstuse, time))) {
204 sb = curr_sb;
205 curr_sb = NULL;
206 } else
207 sb = NULL;
208 spin_unlock_bh(&curr_sb_lock);
209 return sb;
214 * Add an ip_vs_conn information into the current sync_buff.
215 * Called by ip_vs_in.
217 void ip_vs_sync_conn(struct ip_vs_conn *cp)
219 struct ip_vs_sync_mesg *m;
220 struct ip_vs_sync_conn *s;
221 int len;
223 spin_lock(&curr_sb_lock);
224 if (!curr_sb) {
225 if (!(curr_sb=ip_vs_sync_buff_create())) {
226 spin_unlock(&curr_sb_lock);
227 IP_VS_ERR("ip_vs_sync_buff_create failed.\n");
228 return;
232 len = (cp->flags & IP_VS_CONN_F_SEQ_MASK) ? FULL_CONN_SIZE :
233 SIMPLE_CONN_SIZE;
234 m = curr_sb->mesg;
235 s = (struct ip_vs_sync_conn *)curr_sb->head;
237 /* copy members */
238 s->protocol = cp->protocol;
239 s->cport = cp->cport;
240 s->vport = cp->vport;
241 s->dport = cp->dport;
242 s->caddr = cp->caddr;
243 s->vaddr = cp->vaddr;
244 s->daddr = cp->daddr;
245 s->flags = htons(cp->flags & ~IP_VS_CONN_F_HASHED);
246 s->state = htons(cp->state);
247 if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
248 struct ip_vs_sync_conn_options *opt =
249 (struct ip_vs_sync_conn_options *)&s[1];
250 memcpy(opt, &cp->in_seq, sizeof(*opt));
253 m->nr_conns++;
254 m->size += len;
255 curr_sb->head += len;
257 /* check if there is a space for next one */
258 if (curr_sb->head+FULL_CONN_SIZE > curr_sb->end) {
259 sb_queue_tail(curr_sb);
260 curr_sb = NULL;
262 spin_unlock(&curr_sb_lock);
264 /* synchronize its controller if it has */
265 if (cp->control)
266 ip_vs_sync_conn(cp->control);
271 * Process received multicast message and create the corresponding
272 * ip_vs_conn entries.
274 static void ip_vs_process_message(const char *buffer, const size_t buflen)
276 struct ip_vs_sync_mesg *m = (struct ip_vs_sync_mesg *)buffer;
277 struct ip_vs_sync_conn *s;
278 struct ip_vs_sync_conn_options *opt;
279 struct ip_vs_conn *cp;
280 char *p;
281 int i;
283 /* Convert size back to host byte order */
284 m->size = ntohs(m->size);
286 if (buflen != m->size) {
287 IP_VS_ERR("bogus message\n");
288 return;
291 /* SyncID sanity check */
292 if (ip_vs_backup_syncid != 0 && m->syncid != ip_vs_backup_syncid) {
293 IP_VS_DBG(7, "Ignoring incoming msg with syncid = %d\n",
294 m->syncid);
295 return;
298 p = (char *)buffer + sizeof(struct ip_vs_sync_mesg);
299 for (i=0; i<m->nr_conns; i++) {
300 s = (struct ip_vs_sync_conn *)p;
301 cp = ip_vs_conn_in_get(s->protocol,
302 s->caddr, s->cport,
303 s->vaddr, s->vport);
304 if (!cp) {
305 cp = ip_vs_conn_new(s->protocol,
306 s->caddr, s->cport,
307 s->vaddr, s->vport,
308 s->daddr, s->dport,
309 ntohs(s->flags), NULL);
310 if (!cp) {
311 IP_VS_ERR("ip_vs_conn_new failed\n");
312 return;
314 cp->state = ntohs(s->state);
315 } else if (!cp->dest) {
316 /* it is an entry created by the synchronization */
317 cp->state = ntohs(s->state);
318 cp->flags = ntohs(s->flags) | IP_VS_CONN_F_HASHED;
319 } /* Note that we don't touch its state and flags
320 if it is a normal entry. */
322 if (ntohs(s->flags) & IP_VS_CONN_F_SEQ_MASK) {
323 opt = (struct ip_vs_sync_conn_options *)&s[1];
324 memcpy(&cp->in_seq, opt, sizeof(*opt));
325 p += FULL_CONN_SIZE;
326 } else
327 p += SIMPLE_CONN_SIZE;
329 atomic_set(&cp->in_pkts, sysctl_ip_vs_sync_threshold[0]);
330 cp->timeout = IP_VS_SYNC_CONN_TIMEOUT;
331 ip_vs_conn_put(cp);
333 if (p > buffer+buflen) {
334 IP_VS_ERR("bogus message\n");
335 return;
342 * Setup loopback of outgoing multicasts on a sending socket
344 static void set_mcast_loop(struct sock *sk, u_char loop)
346 struct inet_sock *inet = inet_sk(sk);
348 /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
349 lock_sock(sk);
350 inet->mc_loop = loop ? 1 : 0;
351 release_sock(sk);
355 * Specify TTL for outgoing multicasts on a sending socket
357 static void set_mcast_ttl(struct sock *sk, u_char ttl)
359 struct inet_sock *inet = inet_sk(sk);
361 /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
362 lock_sock(sk);
363 inet->mc_ttl = ttl;
364 release_sock(sk);
368 * Specifiy default interface for outgoing multicasts
370 static int set_mcast_if(struct sock *sk, char *ifname)
372 struct net_device *dev;
373 struct inet_sock *inet = inet_sk(sk);
375 if ((dev = __dev_get_by_name(ifname)) == NULL)
376 return -ENODEV;
378 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
379 return -EINVAL;
381 lock_sock(sk);
382 inet->mc_index = dev->ifindex;
383 /* inet->mc_addr = 0; */
384 release_sock(sk);
386 return 0;
391 * Set the maximum length of sync message according to the
392 * specified interface's MTU.
394 static int set_sync_mesg_maxlen(int sync_state)
396 struct net_device *dev;
397 int num;
399 if (sync_state == IP_VS_STATE_MASTER) {
400 if ((dev = __dev_get_by_name(ip_vs_master_mcast_ifn)) == NULL)
401 return -ENODEV;
403 num = (dev->mtu - sizeof(struct iphdr) -
404 sizeof(struct udphdr) -
405 SYNC_MESG_HEADER_LEN - 20) / SIMPLE_CONN_SIZE;
406 sync_send_mesg_maxlen =
407 SYNC_MESG_HEADER_LEN + SIMPLE_CONN_SIZE * num;
408 IP_VS_DBG(7, "setting the maximum length of sync sending "
409 "message %d.\n", sync_send_mesg_maxlen);
410 } else if (sync_state == IP_VS_STATE_BACKUP) {
411 if ((dev = __dev_get_by_name(ip_vs_backup_mcast_ifn)) == NULL)
412 return -ENODEV;
414 sync_recv_mesg_maxlen = dev->mtu -
415 sizeof(struct iphdr) - sizeof(struct udphdr);
416 IP_VS_DBG(7, "setting the maximum length of sync receiving "
417 "message %d.\n", sync_recv_mesg_maxlen);
420 return 0;
425 * Join a multicast group.
426 * the group is specified by a class D multicast address 224.0.0.0/8
427 * in the in_addr structure passed in as a parameter.
429 static int
430 join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
432 struct ip_mreqn mreq;
433 struct net_device *dev;
434 int ret;
436 memset(&mreq, 0, sizeof(mreq));
437 memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
439 if ((dev = __dev_get_by_name(ifname)) == NULL)
440 return -ENODEV;
441 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
442 return -EINVAL;
444 mreq.imr_ifindex = dev->ifindex;
446 lock_sock(sk);
447 ret = ip_mc_join_group(sk, &mreq);
448 release_sock(sk);
450 return ret;
454 static int bind_mcastif_addr(struct socket *sock, char *ifname)
456 struct net_device *dev;
457 u32 addr;
458 struct sockaddr_in sin;
460 if ((dev = __dev_get_by_name(ifname)) == NULL)
461 return -ENODEV;
463 addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
464 if (!addr)
465 IP_VS_ERR("You probably need to specify IP address on "
466 "multicast interface.\n");
468 IP_VS_DBG(7, "binding socket with (%s) %u.%u.%u.%u\n",
469 ifname, NIPQUAD(addr));
471 /* Now bind the socket with the address of multicast interface */
472 sin.sin_family = AF_INET;
473 sin.sin_addr.s_addr = addr;
474 sin.sin_port = 0;
476 return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
480 * Set up sending multicast socket over UDP
482 static struct socket * make_send_sock(void)
484 struct socket *sock;
486 /* First create a socket */
487 if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
488 IP_VS_ERR("Error during creation of socket; terminating\n");
489 return NULL;
492 if (set_mcast_if(sock->sk, ip_vs_master_mcast_ifn) < 0) {
493 IP_VS_ERR("Error setting outbound mcast interface\n");
494 goto error;
497 set_mcast_loop(sock->sk, 0);
498 set_mcast_ttl(sock->sk, 1);
500 if (bind_mcastif_addr(sock, ip_vs_master_mcast_ifn) < 0) {
501 IP_VS_ERR("Error binding address of the mcast interface\n");
502 goto error;
505 if (sock->ops->connect(sock,
506 (struct sockaddr*)&mcast_addr,
507 sizeof(struct sockaddr), 0) < 0) {
508 IP_VS_ERR("Error connecting to the multicast addr\n");
509 goto error;
512 return sock;
514 error:
515 sock_release(sock);
516 return NULL;
521 * Set up receiving multicast socket over UDP
523 static struct socket * make_receive_sock(void)
525 struct socket *sock;
527 /* First create a socket */
528 if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
529 IP_VS_ERR("Error during creation of socket; terminating\n");
530 return NULL;
533 /* it is equivalent to the REUSEADDR option in user-space */
534 sock->sk->sk_reuse = 1;
536 if (sock->ops->bind(sock,
537 (struct sockaddr*)&mcast_addr,
538 sizeof(struct sockaddr)) < 0) {
539 IP_VS_ERR("Error binding to the multicast addr\n");
540 goto error;
543 /* join the multicast group */
544 if (join_mcast_group(sock->sk,
545 (struct in_addr*)&mcast_addr.sin_addr,
546 ip_vs_backup_mcast_ifn) < 0) {
547 IP_VS_ERR("Error joining to the multicast group\n");
548 goto error;
551 return sock;
553 error:
554 sock_release(sock);
555 return NULL;
559 static int
560 ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
562 struct msghdr msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
563 struct kvec iov;
564 int len;
566 EnterFunction(7);
567 iov.iov_base = (void *)buffer;
568 iov.iov_len = length;
570 len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
572 LeaveFunction(7);
573 return len;
576 static void
577 ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
579 int msize;
581 msize = msg->size;
583 /* Put size in network byte order */
584 msg->size = htons(msg->size);
586 if (ip_vs_send_async(sock, (char *)msg, msize) != msize)
587 IP_VS_ERR("ip_vs_send_async error\n");
590 static int
591 ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
593 struct msghdr msg = {NULL,};
594 struct kvec iov;
595 int len;
597 EnterFunction(7);
599 /* Receive a packet */
600 iov.iov_base = buffer;
601 iov.iov_len = (size_t)buflen;
603 len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, 0);
605 if (len < 0)
606 return -1;
608 LeaveFunction(7);
609 return len;
613 static DECLARE_WAIT_QUEUE_HEAD(sync_wait);
614 static pid_t sync_master_pid = 0;
615 static pid_t sync_backup_pid = 0;
617 static DECLARE_WAIT_QUEUE_HEAD(stop_sync_wait);
618 static int stop_master_sync = 0;
619 static int stop_backup_sync = 0;
621 static void sync_master_loop(void)
623 struct socket *sock;
624 struct ip_vs_sync_buff *sb;
626 /* create the sending multicast socket */
627 sock = make_send_sock();
628 if (!sock)
629 return;
631 IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, "
632 "syncid = %d\n",
633 ip_vs_master_mcast_ifn, ip_vs_master_syncid);
635 for (;;) {
636 while ((sb=sb_dequeue())) {
637 ip_vs_send_sync_msg(sock, sb->mesg);
638 ip_vs_sync_buff_release(sb);
641 /* check if entries stay in curr_sb for 2 seconds */
642 if ((sb = get_curr_sync_buff(2*HZ))) {
643 ip_vs_send_sync_msg(sock, sb->mesg);
644 ip_vs_sync_buff_release(sb);
647 if (stop_master_sync)
648 break;
650 ssleep(1);
653 /* clean up the sync_buff queue */
654 while ((sb=sb_dequeue())) {
655 ip_vs_sync_buff_release(sb);
658 /* clean up the current sync_buff */
659 if ((sb = get_curr_sync_buff(0))) {
660 ip_vs_sync_buff_release(sb);
663 /* release the sending multicast socket */
664 sock_release(sock);
668 static void sync_backup_loop(void)
670 struct socket *sock;
671 char *buf;
672 int len;
674 if (!(buf = kmalloc(sync_recv_mesg_maxlen, GFP_ATOMIC))) {
675 IP_VS_ERR("sync_backup_loop: kmalloc error\n");
676 return;
679 /* create the receiving multicast socket */
680 sock = make_receive_sock();
681 if (!sock)
682 goto out;
684 IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
685 "syncid = %d\n",
686 ip_vs_backup_mcast_ifn, ip_vs_backup_syncid);
688 for (;;) {
689 /* do you have data now? */
690 while (!skb_queue_empty(&(sock->sk->sk_receive_queue))) {
691 if ((len =
692 ip_vs_receive(sock, buf,
693 sync_recv_mesg_maxlen)) <= 0) {
694 IP_VS_ERR("receiving message error\n");
695 break;
697 /* disable bottom half, because it accessed the data
698 shared by softirq while getting/creating conns */
699 local_bh_disable();
700 ip_vs_process_message(buf, len);
701 local_bh_enable();
704 if (stop_backup_sync)
705 break;
707 ssleep(1);
710 /* release the sending multicast socket */
711 sock_release(sock);
713 out:
714 kfree(buf);
718 static void set_sync_pid(int sync_state, pid_t sync_pid)
720 if (sync_state == IP_VS_STATE_MASTER)
721 sync_master_pid = sync_pid;
722 else if (sync_state == IP_VS_STATE_BACKUP)
723 sync_backup_pid = sync_pid;
726 static void set_stop_sync(int sync_state, int set)
728 if (sync_state == IP_VS_STATE_MASTER)
729 stop_master_sync = set;
730 else if (sync_state == IP_VS_STATE_BACKUP)
731 stop_backup_sync = set;
732 else {
733 stop_master_sync = set;
734 stop_backup_sync = set;
738 static int sync_thread(void *startup)
740 DECLARE_WAITQUEUE(wait, current);
741 mm_segment_t oldmm;
742 int state;
743 const char *name;
745 /* increase the module use count */
746 ip_vs_use_count_inc();
748 if (ip_vs_sync_state & IP_VS_STATE_MASTER && !sync_master_pid) {
749 state = IP_VS_STATE_MASTER;
750 name = "ipvs_syncmaster";
751 } else if (ip_vs_sync_state & IP_VS_STATE_BACKUP && !sync_backup_pid) {
752 state = IP_VS_STATE_BACKUP;
753 name = "ipvs_syncbackup";
754 } else {
755 IP_VS_BUG();
756 ip_vs_use_count_dec();
757 return -EINVAL;
760 daemonize(name);
762 oldmm = get_fs();
763 set_fs(KERNEL_DS);
765 /* Block all signals */
766 spin_lock_irq(&current->sighand->siglock);
767 siginitsetinv(&current->blocked, 0);
768 recalc_sigpending();
769 spin_unlock_irq(&current->sighand->siglock);
771 /* set the maximum length of sync message */
772 set_sync_mesg_maxlen(state);
774 /* set up multicast address */
775 mcast_addr.sin_family = AF_INET;
776 mcast_addr.sin_port = htons(IP_VS_SYNC_PORT);
777 mcast_addr.sin_addr.s_addr = htonl(IP_VS_SYNC_GROUP);
779 add_wait_queue(&sync_wait, &wait);
781 set_sync_pid(state, current->pid);
782 complete((struct completion *)startup);
784 /* processing master/backup loop here */
785 if (state == IP_VS_STATE_MASTER)
786 sync_master_loop();
787 else if (state == IP_VS_STATE_BACKUP)
788 sync_backup_loop();
789 else IP_VS_BUG();
791 remove_wait_queue(&sync_wait, &wait);
793 /* thread exits */
794 set_sync_pid(state, 0);
795 IP_VS_INFO("sync thread stopped!\n");
797 set_fs(oldmm);
799 /* decrease the module use count */
800 ip_vs_use_count_dec();
802 set_stop_sync(state, 0);
803 wake_up(&stop_sync_wait);
805 return 0;
809 static int fork_sync_thread(void *startup)
811 pid_t pid;
813 /* fork the sync thread here, then the parent process of the
814 sync thread is the init process after this thread exits. */
815 repeat:
816 if ((pid = kernel_thread(sync_thread, startup, 0)) < 0) {
817 IP_VS_ERR("could not create sync_thread due to %d... "
818 "retrying.\n", pid);
819 ssleep(1);
820 goto repeat;
823 return 0;
827 int start_sync_thread(int state, char *mcast_ifn, __u8 syncid)
829 DECLARE_COMPLETION(startup);
830 pid_t pid;
832 if ((state == IP_VS_STATE_MASTER && sync_master_pid) ||
833 (state == IP_VS_STATE_BACKUP && sync_backup_pid))
834 return -EEXIST;
836 IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, current->pid);
837 IP_VS_DBG(7, "Each ip_vs_sync_conn entry need %Zd bytes\n",
838 sizeof(struct ip_vs_sync_conn));
840 ip_vs_sync_state |= state;
841 if (state == IP_VS_STATE_MASTER) {
842 strcpy(ip_vs_master_mcast_ifn, mcast_ifn);
843 ip_vs_master_syncid = syncid;
844 } else {
845 strcpy(ip_vs_backup_mcast_ifn, mcast_ifn);
846 ip_vs_backup_syncid = syncid;
849 repeat:
850 if ((pid = kernel_thread(fork_sync_thread, &startup, 0)) < 0) {
851 IP_VS_ERR("could not create fork_sync_thread due to %d... "
852 "retrying.\n", pid);
853 ssleep(1);
854 goto repeat;
857 wait_for_completion(&startup);
859 return 0;
863 int stop_sync_thread(int state)
865 DECLARE_WAITQUEUE(wait, current);
867 if ((state == IP_VS_STATE_MASTER && !sync_master_pid) ||
868 (state == IP_VS_STATE_BACKUP && !sync_backup_pid))
869 return -ESRCH;
871 IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, current->pid);
872 IP_VS_INFO("stopping sync thread %d ...\n",
873 (state == IP_VS_STATE_MASTER) ? sync_master_pid : sync_backup_pid);
875 __set_current_state(TASK_UNINTERRUPTIBLE);
876 add_wait_queue(&stop_sync_wait, &wait);
877 set_stop_sync(state, 1);
878 ip_vs_sync_state -= state;
879 wake_up(&sync_wait);
880 schedule();
881 __set_current_state(TASK_RUNNING);
882 remove_wait_queue(&stop_sync_wait, &wait);
884 /* Note: no need to reap the sync thread, because its parent
885 process is the init process */
887 if ((state == IP_VS_STATE_MASTER && stop_master_sync) ||
888 (state == IP_VS_STATE_BACKUP && stop_backup_sync))
889 IP_VS_BUG();
891 return 0;