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
8 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
10 * ip_vs_sync: sync connection info from master load balancer to backups
14 * Alexandre Cassen : Added master & backup support at a time.
15 * Alexandre Cassen : Added SyncID support for incoming sync
17 * Justin Ossevoort : Fix endian problem on sync message size.
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/inetdevice.h>
23 #include <linux/net.h>
24 #include <linux/completion.h>
25 #include <linux/delay.h>
26 #include <linux/skbuff.h>
28 #include <linux/igmp.h> /* for ip_mc_join_group */
29 #include <linux/udp.h>
30 #include <linux/err.h>
34 #include <asm/uaccess.h> /* for get_fs and set_fs */
36 #include <net/ip_vs.h>
38 #define IP_VS_SYNC_GROUP 0xe0000051 /* multicast addr - 224.0.0.81 */
39 #define IP_VS_SYNC_PORT 8848 /* multicast port */
43 * IPVS sync connection entry
45 struct ip_vs_sync_conn {
48 /* Protocol, addresses and port numbers */
49 __u8 protocol; /* Which protocol (TCP/UDP) */
53 __be32 caddr; /* client address */
54 __be32 vaddr; /* virtual address */
55 __be32 daddr; /* destination address */
57 /* Flags and state transition */
58 __be16 flags; /* status flags */
59 __be16 state; /* state info */
61 /* The sequence options start here */
64 struct ip_vs_sync_conn_options {
65 struct ip_vs_seq in_seq; /* incoming seq. struct */
66 struct ip_vs_seq out_seq; /* outgoing seq. struct */
69 struct ip_vs_sync_thread_data {
70 struct completion *startup;
74 #define SIMPLE_CONN_SIZE (sizeof(struct ip_vs_sync_conn))
75 #define FULL_CONN_SIZE \
76 (sizeof(struct ip_vs_sync_conn) + sizeof(struct ip_vs_sync_conn_options))
80 The master mulitcasts messages to the backup load balancers in the
84 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
85 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
86 | Count Conns | SyncID | Size |
87 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
89 | IPVS Sync Connection (1) |
90 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96 | IPVS Sync Connection (n) |
97 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100 #define SYNC_MESG_HEADER_LEN 4
102 struct ip_vs_sync_mesg {
107 /* ip_vs_sync_conn entries start here */
110 /* the maximum length of sync (sending/receiving) message */
111 static int sync_send_mesg_maxlen;
112 static int sync_recv_mesg_maxlen;
114 struct ip_vs_sync_buff {
115 struct list_head list;
116 unsigned long firstuse;
118 /* pointers for the message data */
119 struct ip_vs_sync_mesg *mesg;
125 /* the sync_buff list head and the lock */
126 static LIST_HEAD(ip_vs_sync_queue);
127 static DEFINE_SPINLOCK(ip_vs_sync_lock);
129 /* current sync_buff for accepting new conn entries */
130 static struct ip_vs_sync_buff *curr_sb = NULL;
131 static DEFINE_SPINLOCK(curr_sb_lock);
133 /* ipvs sync daemon state */
134 volatile int ip_vs_sync_state = IP_VS_STATE_NONE;
135 volatile int ip_vs_master_syncid = 0;
136 volatile int ip_vs_backup_syncid = 0;
138 /* multicast interface name */
139 char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
140 char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
143 static struct sockaddr_in mcast_addr = {
144 .sin_family = AF_INET,
145 .sin_port = __constant_htons(IP_VS_SYNC_PORT),
146 .sin_addr.s_addr = __constant_htonl(IP_VS_SYNC_GROUP),
150 static inline void sb_queue_tail(struct ip_vs_sync_buff *sb)
152 spin_lock(&ip_vs_sync_lock);
153 list_add_tail(&sb->list, &ip_vs_sync_queue);
154 spin_unlock(&ip_vs_sync_lock);
157 static inline struct ip_vs_sync_buff * sb_dequeue(void)
159 struct ip_vs_sync_buff *sb;
161 spin_lock_bh(&ip_vs_sync_lock);
162 if (list_empty(&ip_vs_sync_queue)) {
165 sb = list_entry(ip_vs_sync_queue.next,
166 struct ip_vs_sync_buff,
170 spin_unlock_bh(&ip_vs_sync_lock);
175 static inline struct ip_vs_sync_buff * ip_vs_sync_buff_create(void)
177 struct ip_vs_sync_buff *sb;
179 if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
182 if (!(sb->mesg=kmalloc(sync_send_mesg_maxlen, GFP_ATOMIC))) {
186 sb->mesg->nr_conns = 0;
187 sb->mesg->syncid = ip_vs_master_syncid;
189 sb->head = (unsigned char *)sb->mesg + 4;
190 sb->end = (unsigned char *)sb->mesg + sync_send_mesg_maxlen;
191 sb->firstuse = jiffies;
195 static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff *sb)
202 * Get the current sync buffer if it has been created for more
203 * than the specified time or the specified time is zero.
205 static inline struct ip_vs_sync_buff *
206 get_curr_sync_buff(unsigned long time)
208 struct ip_vs_sync_buff *sb;
210 spin_lock_bh(&curr_sb_lock);
211 if (curr_sb && (time == 0 ||
212 time_before(jiffies - curr_sb->firstuse, time))) {
217 spin_unlock_bh(&curr_sb_lock);
223 * Add an ip_vs_conn information into the current sync_buff.
224 * Called by ip_vs_in.
226 void ip_vs_sync_conn(struct ip_vs_conn *cp)
228 struct ip_vs_sync_mesg *m;
229 struct ip_vs_sync_conn *s;
232 spin_lock(&curr_sb_lock);
234 if (!(curr_sb=ip_vs_sync_buff_create())) {
235 spin_unlock(&curr_sb_lock);
236 IP_VS_ERR("ip_vs_sync_buff_create failed.\n");
241 len = (cp->flags & IP_VS_CONN_F_SEQ_MASK) ? FULL_CONN_SIZE :
244 s = (struct ip_vs_sync_conn *)curr_sb->head;
247 s->protocol = cp->protocol;
248 s->cport = cp->cport;
249 s->vport = cp->vport;
250 s->dport = cp->dport;
251 s->caddr = cp->caddr;
252 s->vaddr = cp->vaddr;
253 s->daddr = cp->daddr;
254 s->flags = htons(cp->flags & ~IP_VS_CONN_F_HASHED);
255 s->state = htons(cp->state);
256 if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
257 struct ip_vs_sync_conn_options *opt =
258 (struct ip_vs_sync_conn_options *)&s[1];
259 memcpy(opt, &cp->in_seq, sizeof(*opt));
264 curr_sb->head += len;
266 /* check if there is a space for next one */
267 if (curr_sb->head+FULL_CONN_SIZE > curr_sb->end) {
268 sb_queue_tail(curr_sb);
271 spin_unlock(&curr_sb_lock);
273 /* synchronize its controller if it has */
275 ip_vs_sync_conn(cp->control);
280 * Process received multicast message and create the corresponding
281 * ip_vs_conn entries.
283 static void ip_vs_process_message(const char *buffer, const size_t buflen)
285 struct ip_vs_sync_mesg *m = (struct ip_vs_sync_mesg *)buffer;
286 struct ip_vs_sync_conn *s;
287 struct ip_vs_sync_conn_options *opt;
288 struct ip_vs_conn *cp;
289 struct ip_vs_protocol *pp;
290 struct ip_vs_dest *dest;
294 if (buflen < sizeof(struct ip_vs_sync_mesg)) {
295 IP_VS_ERR_RL("sync message header too short\n");
299 /* Convert size back to host byte order */
300 m->size = ntohs(m->size);
302 if (buflen != m->size) {
303 IP_VS_ERR_RL("bogus sync message size\n");
307 /* SyncID sanity check */
308 if (ip_vs_backup_syncid != 0 && m->syncid != ip_vs_backup_syncid) {
309 IP_VS_DBG(7, "Ignoring incoming msg with syncid = %d\n",
314 p = (char *)buffer + sizeof(struct ip_vs_sync_mesg);
315 for (i=0; i<m->nr_conns; i++) {
316 unsigned flags, state;
318 if (p + SIMPLE_CONN_SIZE > buffer+buflen) {
319 IP_VS_ERR_RL("bogus conn in sync message\n");
322 s = (struct ip_vs_sync_conn *) p;
323 flags = ntohs(s->flags) | IP_VS_CONN_F_SYNC;
324 flags &= ~IP_VS_CONN_F_HASHED;
325 if (flags & IP_VS_CONN_F_SEQ_MASK) {
326 opt = (struct ip_vs_sync_conn_options *)&s[1];
328 if (p > buffer+buflen) {
329 IP_VS_ERR_RL("bogus conn options in sync message\n");
334 p += SIMPLE_CONN_SIZE;
337 state = ntohs(s->state);
338 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
339 pp = ip_vs_proto_get(s->protocol);
341 IP_VS_ERR_RL("Unsupported protocol %u in sync msg\n",
345 if (state >= pp->num_states) {
346 IP_VS_DBG(2, "Invalid %s state %u in sync msg\n",
351 /* protocol in templates is not used for state/timeout */
354 IP_VS_DBG(2, "Invalid template state %u in sync msg\n",
360 if (!(flags & IP_VS_CONN_F_TEMPLATE))
361 cp = ip_vs_conn_in_get(s->protocol,
365 cp = ip_vs_ct_in_get(s->protocol,
370 * Find the appropriate destination for the connection.
371 * If it is not found the connection will remain unbound
374 dest = ip_vs_find_dest(s->daddr, s->dport,
377 /* Set the approprite ativity flag */
378 if (s->protocol == IPPROTO_TCP) {
379 if (state != IP_VS_TCP_S_ESTABLISHED)
380 flags |= IP_VS_CONN_F_INACTIVE;
382 flags &= ~IP_VS_CONN_F_INACTIVE;
384 cp = ip_vs_conn_new(s->protocol,
390 atomic_dec(&dest->refcnt);
392 IP_VS_ERR("ip_vs_conn_new failed\n");
395 } else if (!cp->dest) {
396 dest = ip_vs_try_bind_dest(cp);
398 atomic_dec(&dest->refcnt);
399 } else if ((cp->dest) && (cp->protocol == IPPROTO_TCP) &&
400 (cp->state != state)) {
401 /* update active/inactive flag for the connection */
403 if (!(cp->flags & IP_VS_CONN_F_INACTIVE) &&
404 (state != IP_VS_TCP_S_ESTABLISHED)) {
405 atomic_dec(&dest->activeconns);
406 atomic_inc(&dest->inactconns);
407 cp->flags |= IP_VS_CONN_F_INACTIVE;
408 } else if ((cp->flags & IP_VS_CONN_F_INACTIVE) &&
409 (state == IP_VS_TCP_S_ESTABLISHED)) {
410 atomic_inc(&dest->activeconns);
411 atomic_dec(&dest->inactconns);
412 cp->flags &= ~IP_VS_CONN_F_INACTIVE;
417 memcpy(&cp->in_seq, opt, sizeof(*opt));
418 atomic_set(&cp->in_pkts, sysctl_ip_vs_sync_threshold[0]);
420 cp->old_state = cp->state;
422 * We can not recover the right timeout for templates
423 * in all cases, we can not find the right fwmark
424 * virtual service. If needed, we can do it for
425 * non-fwmark persistent services.
427 if (!(flags & IP_VS_CONN_F_TEMPLATE) && pp->timeout_table)
428 cp->timeout = pp->timeout_table[state];
430 cp->timeout = (3*60*HZ);
437 * Setup loopback of outgoing multicasts on a sending socket
439 static void set_mcast_loop(struct sock *sk, u_char loop)
441 struct inet_sock *inet = inet_sk(sk);
443 /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
445 inet->mc_loop = loop ? 1 : 0;
450 * Specify TTL for outgoing multicasts on a sending socket
452 static void set_mcast_ttl(struct sock *sk, u_char ttl)
454 struct inet_sock *inet = inet_sk(sk);
456 /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
463 * Specifiy default interface for outgoing multicasts
465 static int set_mcast_if(struct sock *sk, char *ifname)
467 struct net_device *dev;
468 struct inet_sock *inet = inet_sk(sk);
470 if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
473 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
477 inet->mc_index = dev->ifindex;
478 /* inet->mc_addr = 0; */
486 * Set the maximum length of sync message according to the
487 * specified interface's MTU.
489 static int set_sync_mesg_maxlen(int sync_state)
491 struct net_device *dev;
494 if (sync_state == IP_VS_STATE_MASTER) {
495 if ((dev = __dev_get_by_name(&init_net, ip_vs_master_mcast_ifn)) == NULL)
498 num = (dev->mtu - sizeof(struct iphdr) -
499 sizeof(struct udphdr) -
500 SYNC_MESG_HEADER_LEN - 20) / SIMPLE_CONN_SIZE;
501 sync_send_mesg_maxlen =
502 SYNC_MESG_HEADER_LEN + SIMPLE_CONN_SIZE * num;
503 IP_VS_DBG(7, "setting the maximum length of sync sending "
504 "message %d.\n", sync_send_mesg_maxlen);
505 } else if (sync_state == IP_VS_STATE_BACKUP) {
506 if ((dev = __dev_get_by_name(&init_net, ip_vs_backup_mcast_ifn)) == NULL)
509 sync_recv_mesg_maxlen = dev->mtu -
510 sizeof(struct iphdr) - sizeof(struct udphdr);
511 IP_VS_DBG(7, "setting the maximum length of sync receiving "
512 "message %d.\n", sync_recv_mesg_maxlen);
520 * Join a multicast group.
521 * the group is specified by a class D multicast address 224.0.0.0/8
522 * in the in_addr structure passed in as a parameter.
525 join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
527 struct ip_mreqn mreq;
528 struct net_device *dev;
531 memset(&mreq, 0, sizeof(mreq));
532 memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
534 if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
536 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
539 mreq.imr_ifindex = dev->ifindex;
542 ret = ip_mc_join_group(sk, &mreq);
549 static int bind_mcastif_addr(struct socket *sock, char *ifname)
551 struct net_device *dev;
553 struct sockaddr_in sin;
555 if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
558 addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
560 IP_VS_ERR("You probably need to specify IP address on "
561 "multicast interface.\n");
563 IP_VS_DBG(7, "binding socket with (%s) %u.%u.%u.%u\n",
564 ifname, NIPQUAD(addr));
566 /* Now bind the socket with the address of multicast interface */
567 sin.sin_family = AF_INET;
568 sin.sin_addr.s_addr = addr;
571 return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
575 * Set up sending multicast socket over UDP
577 static struct socket * make_send_sock(void)
582 /* First create a socket */
583 result = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
585 IP_VS_ERR("Error during creation of socket; terminating\n");
586 return ERR_PTR(result);
589 result = set_mcast_if(sock->sk, ip_vs_master_mcast_ifn);
591 IP_VS_ERR("Error setting outbound mcast interface\n");
595 set_mcast_loop(sock->sk, 0);
596 set_mcast_ttl(sock->sk, 1);
598 result = bind_mcastif_addr(sock, ip_vs_master_mcast_ifn);
600 IP_VS_ERR("Error binding address of the mcast interface\n");
604 result = sock->ops->connect(sock, (struct sockaddr *) &mcast_addr,
605 sizeof(struct sockaddr), 0);
607 IP_VS_ERR("Error connecting to the multicast addr\n");
615 return ERR_PTR(result);
620 * Set up receiving multicast socket over UDP
622 static struct socket * make_receive_sock(void)
627 /* First create a socket */
628 result = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
630 IP_VS_ERR("Error during creation of socket; terminating\n");
631 return ERR_PTR(result);
634 /* it is equivalent to the REUSEADDR option in user-space */
635 sock->sk->sk_reuse = 1;
637 result = sock->ops->bind(sock, (struct sockaddr *) &mcast_addr,
638 sizeof(struct sockaddr));
640 IP_VS_ERR("Error binding to the multicast addr\n");
644 /* join the multicast group */
645 result = join_mcast_group(sock->sk,
646 (struct in_addr *) &mcast_addr.sin_addr,
647 ip_vs_backup_mcast_ifn);
649 IP_VS_ERR("Error joining to the multicast group\n");
657 return ERR_PTR(result);
662 ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
664 struct msghdr msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
669 iov.iov_base = (void *)buffer;
670 iov.iov_len = length;
672 len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
679 ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
685 /* Put size in network byte order */
686 msg->size = htons(msg->size);
688 if (ip_vs_send_async(sock, (char *)msg, msize) != msize)
689 IP_VS_ERR("ip_vs_send_async error\n");
693 ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
695 struct msghdr msg = {NULL,};
701 /* Receive a packet */
702 iov.iov_base = buffer;
703 iov.iov_len = (size_t)buflen;
705 len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, 0);
715 static DECLARE_WAIT_QUEUE_HEAD(sync_wait);
716 static pid_t sync_master_pid = 0;
717 static pid_t sync_backup_pid = 0;
719 static DECLARE_WAIT_QUEUE_HEAD(stop_sync_wait);
720 static int stop_master_sync = 0;
721 static int stop_backup_sync = 0;
723 static void sync_master_loop(void)
726 struct ip_vs_sync_buff *sb;
728 /* create the sending multicast socket */
729 sock = make_send_sock();
733 IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, "
735 ip_vs_master_mcast_ifn, ip_vs_master_syncid);
738 while ((sb=sb_dequeue())) {
739 ip_vs_send_sync_msg(sock, sb->mesg);
740 ip_vs_sync_buff_release(sb);
743 /* check if entries stay in curr_sb for 2 seconds */
744 if ((sb = get_curr_sync_buff(2*HZ))) {
745 ip_vs_send_sync_msg(sock, sb->mesg);
746 ip_vs_sync_buff_release(sb);
749 if (stop_master_sync)
752 msleep_interruptible(1000);
755 /* clean up the sync_buff queue */
756 while ((sb=sb_dequeue())) {
757 ip_vs_sync_buff_release(sb);
760 /* clean up the current sync_buff */
761 if ((sb = get_curr_sync_buff(0))) {
762 ip_vs_sync_buff_release(sb);
765 /* release the sending multicast socket */
770 static void sync_backup_loop(void)
776 if (!(buf = kmalloc(sync_recv_mesg_maxlen, GFP_ATOMIC))) {
777 IP_VS_ERR("sync_backup_loop: kmalloc error\n");
781 /* create the receiving multicast socket */
782 sock = make_receive_sock();
786 IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
788 ip_vs_backup_mcast_ifn, ip_vs_backup_syncid);
791 /* do you have data now? */
792 while (!skb_queue_empty(&(sock->sk->sk_receive_queue))) {
794 ip_vs_receive(sock, buf,
795 sync_recv_mesg_maxlen)) <= 0) {
796 IP_VS_ERR("receiving message error\n");
799 /* disable bottom half, because it accessed the data
800 shared by softirq while getting/creating conns */
802 ip_vs_process_message(buf, len);
806 if (stop_backup_sync)
809 msleep_interruptible(1000);
812 /* release the sending multicast socket */
820 static void set_sync_pid(int sync_state, pid_t sync_pid)
822 if (sync_state == IP_VS_STATE_MASTER)
823 sync_master_pid = sync_pid;
824 else if (sync_state == IP_VS_STATE_BACKUP)
825 sync_backup_pid = sync_pid;
828 static void set_stop_sync(int sync_state, int set)
830 if (sync_state == IP_VS_STATE_MASTER)
831 stop_master_sync = set;
832 else if (sync_state == IP_VS_STATE_BACKUP)
833 stop_backup_sync = set;
835 stop_master_sync = set;
836 stop_backup_sync = set;
840 static int sync_thread(void *startup)
842 DECLARE_WAITQUEUE(wait, current);
846 struct ip_vs_sync_thread_data *tinfo = startup;
848 /* increase the module use count */
849 ip_vs_use_count_inc();
851 if (ip_vs_sync_state & IP_VS_STATE_MASTER && !sync_master_pid) {
852 state = IP_VS_STATE_MASTER;
853 name = "ipvs_syncmaster";
854 } else if (ip_vs_sync_state & IP_VS_STATE_BACKUP && !sync_backup_pid) {
855 state = IP_VS_STATE_BACKUP;
856 name = "ipvs_syncbackup";
859 ip_vs_use_count_dec();
868 /* Block all signals */
869 spin_lock_irq(¤t->sighand->siglock);
870 siginitsetinv(¤t->blocked, 0);
872 spin_unlock_irq(¤t->sighand->siglock);
874 /* set the maximum length of sync message */
875 set_sync_mesg_maxlen(state);
877 add_wait_queue(&sync_wait, &wait);
879 set_sync_pid(state, task_pid_nr(current));
880 complete(tinfo->startup);
883 * once we call the completion queue above, we should
884 * null out that reference, since its allocated on the
885 * stack of the creating kernel thread
887 tinfo->startup = NULL;
889 /* processing master/backup loop here */
890 if (state == IP_VS_STATE_MASTER)
892 else if (state == IP_VS_STATE_BACKUP)
896 remove_wait_queue(&sync_wait, &wait);
901 * If we weren't explicitly stopped, then we
902 * exited in error, and should undo our state
904 if ((!stop_master_sync) && (!stop_backup_sync))
905 ip_vs_sync_state -= tinfo->state;
907 set_sync_pid(state, 0);
908 IP_VS_INFO("sync thread stopped!\n");
912 /* decrease the module use count */
913 ip_vs_use_count_dec();
915 set_stop_sync(state, 0);
916 wake_up(&stop_sync_wait);
919 * we need to free the structure that was allocated
920 * for us in start_sync_thread
927 static int fork_sync_thread(void *startup)
931 /* fork the sync thread here, then the parent process of the
932 sync thread is the init process after this thread exits. */
934 if ((pid = kernel_thread(sync_thread, startup, 0)) < 0) {
935 IP_VS_ERR("could not create sync_thread due to %d... "
937 msleep_interruptible(1000);
945 int start_sync_thread(int state, char *mcast_ifn, __u8 syncid)
947 DECLARE_COMPLETION_ONSTACK(startup);
949 struct ip_vs_sync_thread_data *tinfo;
951 if ((state == IP_VS_STATE_MASTER && sync_master_pid) ||
952 (state == IP_VS_STATE_BACKUP && sync_backup_pid))
956 * Note that tinfo will be freed in sync_thread on exit
958 tinfo = kmalloc(sizeof(struct ip_vs_sync_thread_data), GFP_KERNEL);
962 IP_VS_DBG(7, "%s: pid %d\n", __func__, task_pid_nr(current));
963 IP_VS_DBG(7, "Each ip_vs_sync_conn entry need %Zd bytes\n",
964 sizeof(struct ip_vs_sync_conn));
966 ip_vs_sync_state |= state;
967 if (state == IP_VS_STATE_MASTER) {
968 strlcpy(ip_vs_master_mcast_ifn, mcast_ifn,
969 sizeof(ip_vs_master_mcast_ifn));
970 ip_vs_master_syncid = syncid;
972 strlcpy(ip_vs_backup_mcast_ifn, mcast_ifn,
973 sizeof(ip_vs_backup_mcast_ifn));
974 ip_vs_backup_syncid = syncid;
977 tinfo->state = state;
978 tinfo->startup = &startup;
981 if ((pid = kernel_thread(fork_sync_thread, tinfo, 0)) < 0) {
982 IP_VS_ERR("could not create fork_sync_thread due to %d... "
984 msleep_interruptible(1000);
988 wait_for_completion(&startup);
994 int stop_sync_thread(int state)
996 DECLARE_WAITQUEUE(wait, current);
998 if ((state == IP_VS_STATE_MASTER && !sync_master_pid) ||
999 (state == IP_VS_STATE_BACKUP && !sync_backup_pid))
1002 IP_VS_DBG(7, "%s: pid %d\n", __func__, task_pid_nr(current));
1003 IP_VS_INFO("stopping sync thread %d ...\n",
1004 (state == IP_VS_STATE_MASTER) ?
1005 sync_master_pid : sync_backup_pid);
1007 __set_current_state(TASK_UNINTERRUPTIBLE);
1008 add_wait_queue(&stop_sync_wait, &wait);
1009 set_stop_sync(state, 1);
1010 ip_vs_sync_state -= state;
1011 wake_up(&sync_wait);
1013 __set_current_state(TASK_RUNNING);
1014 remove_wait_queue(&stop_sync_wait, &wait);
1016 /* Note: no need to reap the sync thread, because its parent
1017 process is the init process */
1019 if ((state == IP_VS_STATE_MASTER && stop_master_sync) ||
1020 (state == IP_VS_STATE_BACKUP && stop_backup_sync))