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 * 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
16 * Alexandre Cassen : Added master & backup support at a time.
17 * Alexandre Cassen : Added SyncID support for incoming sync
19 * Justin Ossevoort : Fix endian problem on sync message size.
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>
30 #include <linux/igmp.h> /* for ip_mc_join_group */
31 #include <linux/udp.h>
35 #include <asm/uaccess.h> /* for get_fs and set_fs */
37 #include <net/ip_vs.h>
39 #define IP_VS_SYNC_GROUP 0xe0000051 /* multicast addr - 224.0.0.81 */
40 #define IP_VS_SYNC_PORT 8848 /* multicast port */
44 * IPVS sync connection entry
46 struct ip_vs_sync_conn
{
49 /* Protocol, addresses and port numbers */
50 __u8 protocol
; /* Which protocol (TCP/UDP) */
54 __be32 caddr
; /* client address */
55 __be32 vaddr
; /* virtual address */
56 __be32 daddr
; /* destination address */
58 /* Flags and state transition */
59 __be16 flags
; /* status flags */
60 __be16 state
; /* state info */
62 /* The sequence options start here */
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 */
70 struct ip_vs_sync_thread_data
{
71 struct completion
*startup
;
75 #define IP_VS_SYNC_CONN_TIMEOUT (3*60*HZ)
76 #define SIMPLE_CONN_SIZE (sizeof(struct ip_vs_sync_conn))
77 #define FULL_CONN_SIZE \
78 (sizeof(struct ip_vs_sync_conn) + sizeof(struct ip_vs_sync_conn_options))
82 The master mulitcasts messages to the backup load balancers in the
86 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
87 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
88 | Count Conns | SyncID | Size |
89 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91 | IPVS Sync Connection (1) |
92 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98 | IPVS Sync Connection (n) |
99 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102 #define SYNC_MESG_HEADER_LEN 4
104 struct ip_vs_sync_mesg
{
109 /* ip_vs_sync_conn entries start here */
112 /* the maximum length of sync (sending/receiving) message */
113 static int sync_send_mesg_maxlen
;
114 static int sync_recv_mesg_maxlen
;
116 struct ip_vs_sync_buff
{
117 struct list_head list
;
118 unsigned long firstuse
;
120 /* pointers for the message data */
121 struct ip_vs_sync_mesg
*mesg
;
127 /* the sync_buff list head and the lock */
128 static LIST_HEAD(ip_vs_sync_queue
);
129 static DEFINE_SPINLOCK(ip_vs_sync_lock
);
131 /* current sync_buff for accepting new conn entries */
132 static struct ip_vs_sync_buff
*curr_sb
= NULL
;
133 static DEFINE_SPINLOCK(curr_sb_lock
);
135 /* ipvs sync daemon state */
136 volatile int ip_vs_sync_state
= IP_VS_STATE_NONE
;
137 volatile int ip_vs_master_syncid
= 0;
138 volatile int ip_vs_backup_syncid
= 0;
140 /* multicast interface name */
141 char ip_vs_master_mcast_ifn
[IP_VS_IFNAME_MAXLEN
];
142 char ip_vs_backup_mcast_ifn
[IP_VS_IFNAME_MAXLEN
];
145 static struct sockaddr_in mcast_addr
;
148 static inline void sb_queue_tail(struct ip_vs_sync_buff
*sb
)
150 spin_lock(&ip_vs_sync_lock
);
151 list_add_tail(&sb
->list
, &ip_vs_sync_queue
);
152 spin_unlock(&ip_vs_sync_lock
);
155 static inline struct ip_vs_sync_buff
* sb_dequeue(void)
157 struct ip_vs_sync_buff
*sb
;
159 spin_lock_bh(&ip_vs_sync_lock
);
160 if (list_empty(&ip_vs_sync_queue
)) {
163 sb
= list_entry(ip_vs_sync_queue
.next
,
164 struct ip_vs_sync_buff
,
168 spin_unlock_bh(&ip_vs_sync_lock
);
173 static inline struct ip_vs_sync_buff
* ip_vs_sync_buff_create(void)
175 struct ip_vs_sync_buff
*sb
;
177 if (!(sb
=kmalloc(sizeof(struct ip_vs_sync_buff
), GFP_ATOMIC
)))
180 if (!(sb
->mesg
=kmalloc(sync_send_mesg_maxlen
, GFP_ATOMIC
))) {
184 sb
->mesg
->nr_conns
= 0;
185 sb
->mesg
->syncid
= ip_vs_master_syncid
;
187 sb
->head
= (unsigned char *)sb
->mesg
+ 4;
188 sb
->end
= (unsigned char *)sb
->mesg
+ sync_send_mesg_maxlen
;
189 sb
->firstuse
= jiffies
;
193 static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff
*sb
)
200 * Get the current sync buffer if it has been created for more
201 * than the specified time or the specified time is zero.
203 static inline struct ip_vs_sync_buff
*
204 get_curr_sync_buff(unsigned long time
)
206 struct ip_vs_sync_buff
*sb
;
208 spin_lock_bh(&curr_sb_lock
);
209 if (curr_sb
&& (time
== 0 ||
210 time_before(jiffies
- curr_sb
->firstuse
, time
))) {
215 spin_unlock_bh(&curr_sb_lock
);
221 * Add an ip_vs_conn information into the current sync_buff.
222 * Called by ip_vs_in.
224 void ip_vs_sync_conn(struct ip_vs_conn
*cp
)
226 struct ip_vs_sync_mesg
*m
;
227 struct ip_vs_sync_conn
*s
;
230 spin_lock(&curr_sb_lock
);
232 if (!(curr_sb
=ip_vs_sync_buff_create())) {
233 spin_unlock(&curr_sb_lock
);
234 IP_VS_ERR("ip_vs_sync_buff_create failed.\n");
239 len
= (cp
->flags
& IP_VS_CONN_F_SEQ_MASK
) ? FULL_CONN_SIZE
:
242 s
= (struct ip_vs_sync_conn
*)curr_sb
->head
;
245 s
->protocol
= cp
->protocol
;
246 s
->cport
= cp
->cport
;
247 s
->vport
= cp
->vport
;
248 s
->dport
= cp
->dport
;
249 s
->caddr
= cp
->caddr
;
250 s
->vaddr
= cp
->vaddr
;
251 s
->daddr
= cp
->daddr
;
252 s
->flags
= htons(cp
->flags
& ~IP_VS_CONN_F_HASHED
);
253 s
->state
= htons(cp
->state
);
254 if (cp
->flags
& IP_VS_CONN_F_SEQ_MASK
) {
255 struct ip_vs_sync_conn_options
*opt
=
256 (struct ip_vs_sync_conn_options
*)&s
[1];
257 memcpy(opt
, &cp
->in_seq
, sizeof(*opt
));
262 curr_sb
->head
+= len
;
264 /* check if there is a space for next one */
265 if (curr_sb
->head
+FULL_CONN_SIZE
> curr_sb
->end
) {
266 sb_queue_tail(curr_sb
);
269 spin_unlock(&curr_sb_lock
);
271 /* synchronize its controller if it has */
273 ip_vs_sync_conn(cp
->control
);
278 * Process received multicast message and create the corresponding
279 * ip_vs_conn entries.
281 static void ip_vs_process_message(const char *buffer
, const size_t buflen
)
283 struct ip_vs_sync_mesg
*m
= (struct ip_vs_sync_mesg
*)buffer
;
284 struct ip_vs_sync_conn
*s
;
285 struct ip_vs_sync_conn_options
*opt
;
286 struct ip_vs_conn
*cp
;
290 /* Convert size back to host byte order */
291 m
->size
= ntohs(m
->size
);
293 if (buflen
!= m
->size
) {
294 IP_VS_ERR("bogus message\n");
298 /* SyncID sanity check */
299 if (ip_vs_backup_syncid
!= 0 && m
->syncid
!= ip_vs_backup_syncid
) {
300 IP_VS_DBG(7, "Ignoring incoming msg with syncid = %d\n",
305 p
= (char *)buffer
+ sizeof(struct ip_vs_sync_mesg
);
306 for (i
=0; i
<m
->nr_conns
; i
++) {
309 s
= (struct ip_vs_sync_conn
*)p
;
310 flags
= ntohs(s
->flags
);
311 if (!(flags
& IP_VS_CONN_F_TEMPLATE
))
312 cp
= ip_vs_conn_in_get(s
->protocol
,
316 cp
= ip_vs_ct_in_get(s
->protocol
,
320 cp
= ip_vs_conn_new(s
->protocol
,
326 IP_VS_ERR("ip_vs_conn_new failed\n");
329 cp
->state
= ntohs(s
->state
);
330 } else if (!cp
->dest
) {
331 /* it is an entry created by the synchronization */
332 cp
->state
= ntohs(s
->state
);
333 cp
->flags
= flags
| IP_VS_CONN_F_HASHED
;
334 } /* Note that we don't touch its state and flags
335 if it is a normal entry. */
337 if (flags
& IP_VS_CONN_F_SEQ_MASK
) {
338 opt
= (struct ip_vs_sync_conn_options
*)&s
[1];
339 memcpy(&cp
->in_seq
, opt
, sizeof(*opt
));
342 p
+= SIMPLE_CONN_SIZE
;
344 atomic_set(&cp
->in_pkts
, sysctl_ip_vs_sync_threshold
[0]);
345 cp
->timeout
= IP_VS_SYNC_CONN_TIMEOUT
;
348 if (p
> buffer
+buflen
) {
349 IP_VS_ERR("bogus message\n");
357 * Setup loopback of outgoing multicasts on a sending socket
359 static void set_mcast_loop(struct sock
*sk
, u_char loop
)
361 struct inet_sock
*inet
= inet_sk(sk
);
363 /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
365 inet
->mc_loop
= loop
? 1 : 0;
370 * Specify TTL for outgoing multicasts on a sending socket
372 static void set_mcast_ttl(struct sock
*sk
, u_char ttl
)
374 struct inet_sock
*inet
= inet_sk(sk
);
376 /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
383 * Specifiy default interface for outgoing multicasts
385 static int set_mcast_if(struct sock
*sk
, char *ifname
)
387 struct net_device
*dev
;
388 struct inet_sock
*inet
= inet_sk(sk
);
390 if ((dev
= __dev_get_by_name(&init_net
, ifname
)) == NULL
)
393 if (sk
->sk_bound_dev_if
&& dev
->ifindex
!= sk
->sk_bound_dev_if
)
397 inet
->mc_index
= dev
->ifindex
;
398 /* inet->mc_addr = 0; */
406 * Set the maximum length of sync message according to the
407 * specified interface's MTU.
409 static int set_sync_mesg_maxlen(int sync_state
)
411 struct net_device
*dev
;
414 if (sync_state
== IP_VS_STATE_MASTER
) {
415 if ((dev
= __dev_get_by_name(&init_net
, ip_vs_master_mcast_ifn
)) == NULL
)
418 num
= (dev
->mtu
- sizeof(struct iphdr
) -
419 sizeof(struct udphdr
) -
420 SYNC_MESG_HEADER_LEN
- 20) / SIMPLE_CONN_SIZE
;
421 sync_send_mesg_maxlen
=
422 SYNC_MESG_HEADER_LEN
+ SIMPLE_CONN_SIZE
* num
;
423 IP_VS_DBG(7, "setting the maximum length of sync sending "
424 "message %d.\n", sync_send_mesg_maxlen
);
425 } else if (sync_state
== IP_VS_STATE_BACKUP
) {
426 if ((dev
= __dev_get_by_name(&init_net
, ip_vs_backup_mcast_ifn
)) == NULL
)
429 sync_recv_mesg_maxlen
= dev
->mtu
-
430 sizeof(struct iphdr
) - sizeof(struct udphdr
);
431 IP_VS_DBG(7, "setting the maximum length of sync receiving "
432 "message %d.\n", sync_recv_mesg_maxlen
);
440 * Join a multicast group.
441 * the group is specified by a class D multicast address 224.0.0.0/8
442 * in the in_addr structure passed in as a parameter.
445 join_mcast_group(struct sock
*sk
, struct in_addr
*addr
, char *ifname
)
447 struct ip_mreqn mreq
;
448 struct net_device
*dev
;
451 memset(&mreq
, 0, sizeof(mreq
));
452 memcpy(&mreq
.imr_multiaddr
, addr
, sizeof(struct in_addr
));
454 if ((dev
= __dev_get_by_name(&init_net
, ifname
)) == NULL
)
456 if (sk
->sk_bound_dev_if
&& dev
->ifindex
!= sk
->sk_bound_dev_if
)
459 mreq
.imr_ifindex
= dev
->ifindex
;
462 ret
= ip_mc_join_group(sk
, &mreq
);
469 static int bind_mcastif_addr(struct socket
*sock
, char *ifname
)
471 struct net_device
*dev
;
473 struct sockaddr_in sin
;
475 if ((dev
= __dev_get_by_name(&init_net
, ifname
)) == NULL
)
478 addr
= inet_select_addr(dev
, 0, RT_SCOPE_UNIVERSE
);
480 IP_VS_ERR("You probably need to specify IP address on "
481 "multicast interface.\n");
483 IP_VS_DBG(7, "binding socket with (%s) %u.%u.%u.%u\n",
484 ifname
, NIPQUAD(addr
));
486 /* Now bind the socket with the address of multicast interface */
487 sin
.sin_family
= AF_INET
;
488 sin
.sin_addr
.s_addr
= addr
;
491 return sock
->ops
->bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
));
495 * Set up sending multicast socket over UDP
497 static struct socket
* make_send_sock(void)
501 /* First create a socket */
502 if (sock_create_kern(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
) < 0) {
503 IP_VS_ERR("Error during creation of socket; terminating\n");
507 if (set_mcast_if(sock
->sk
, ip_vs_master_mcast_ifn
) < 0) {
508 IP_VS_ERR("Error setting outbound mcast interface\n");
512 set_mcast_loop(sock
->sk
, 0);
513 set_mcast_ttl(sock
->sk
, 1);
515 if (bind_mcastif_addr(sock
, ip_vs_master_mcast_ifn
) < 0) {
516 IP_VS_ERR("Error binding address of the mcast interface\n");
520 if (sock
->ops
->connect(sock
,
521 (struct sockaddr
*)&mcast_addr
,
522 sizeof(struct sockaddr
), 0) < 0) {
523 IP_VS_ERR("Error connecting to the multicast addr\n");
536 * Set up receiving multicast socket over UDP
538 static struct socket
* make_receive_sock(void)
542 /* First create a socket */
543 if (sock_create_kern(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
) < 0) {
544 IP_VS_ERR("Error during creation of socket; terminating\n");
548 /* it is equivalent to the REUSEADDR option in user-space */
549 sock
->sk
->sk_reuse
= 1;
551 if (sock
->ops
->bind(sock
,
552 (struct sockaddr
*)&mcast_addr
,
553 sizeof(struct sockaddr
)) < 0) {
554 IP_VS_ERR("Error binding to the multicast addr\n");
558 /* join the multicast group */
559 if (join_mcast_group(sock
->sk
,
560 (struct in_addr
*)&mcast_addr
.sin_addr
,
561 ip_vs_backup_mcast_ifn
) < 0) {
562 IP_VS_ERR("Error joining to the multicast group\n");
575 ip_vs_send_async(struct socket
*sock
, const char *buffer
, const size_t length
)
577 struct msghdr msg
= {.msg_flags
= MSG_DONTWAIT
|MSG_NOSIGNAL
};
582 iov
.iov_base
= (void *)buffer
;
583 iov
.iov_len
= length
;
585 len
= kernel_sendmsg(sock
, &msg
, &iov
, 1, (size_t)(length
));
592 ip_vs_send_sync_msg(struct socket
*sock
, struct ip_vs_sync_mesg
*msg
)
598 /* Put size in network byte order */
599 msg
->size
= htons(msg
->size
);
601 if (ip_vs_send_async(sock
, (char *)msg
, msize
) != msize
)
602 IP_VS_ERR("ip_vs_send_async error\n");
606 ip_vs_receive(struct socket
*sock
, char *buffer
, const size_t buflen
)
608 struct msghdr msg
= {NULL
,};
614 /* Receive a packet */
615 iov
.iov_base
= buffer
;
616 iov
.iov_len
= (size_t)buflen
;
618 len
= kernel_recvmsg(sock
, &msg
, &iov
, 1, buflen
, 0);
628 static DECLARE_WAIT_QUEUE_HEAD(sync_wait
);
629 static pid_t sync_master_pid
= 0;
630 static pid_t sync_backup_pid
= 0;
632 static DECLARE_WAIT_QUEUE_HEAD(stop_sync_wait
);
633 static int stop_master_sync
= 0;
634 static int stop_backup_sync
= 0;
636 static void sync_master_loop(void)
639 struct ip_vs_sync_buff
*sb
;
641 /* create the sending multicast socket */
642 sock
= make_send_sock();
646 IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, "
648 ip_vs_master_mcast_ifn
, ip_vs_master_syncid
);
651 while ((sb
=sb_dequeue())) {
652 ip_vs_send_sync_msg(sock
, sb
->mesg
);
653 ip_vs_sync_buff_release(sb
);
656 /* check if entries stay in curr_sb for 2 seconds */
657 if ((sb
= get_curr_sync_buff(2*HZ
))) {
658 ip_vs_send_sync_msg(sock
, sb
->mesg
);
659 ip_vs_sync_buff_release(sb
);
662 if (stop_master_sync
)
665 msleep_interruptible(1000);
668 /* clean up the sync_buff queue */
669 while ((sb
=sb_dequeue())) {
670 ip_vs_sync_buff_release(sb
);
673 /* clean up the current sync_buff */
674 if ((sb
= get_curr_sync_buff(0))) {
675 ip_vs_sync_buff_release(sb
);
678 /* release the sending multicast socket */
683 static void sync_backup_loop(void)
689 if (!(buf
= kmalloc(sync_recv_mesg_maxlen
, GFP_ATOMIC
))) {
690 IP_VS_ERR("sync_backup_loop: kmalloc error\n");
694 /* create the receiving multicast socket */
695 sock
= make_receive_sock();
699 IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
701 ip_vs_backup_mcast_ifn
, ip_vs_backup_syncid
);
704 /* do you have data now? */
705 while (!skb_queue_empty(&(sock
->sk
->sk_receive_queue
))) {
707 ip_vs_receive(sock
, buf
,
708 sync_recv_mesg_maxlen
)) <= 0) {
709 IP_VS_ERR("receiving message error\n");
712 /* disable bottom half, because it accessed the data
713 shared by softirq while getting/creating conns */
715 ip_vs_process_message(buf
, len
);
719 if (stop_backup_sync
)
722 msleep_interruptible(1000);
725 /* release the sending multicast socket */
733 static void set_sync_pid(int sync_state
, pid_t sync_pid
)
735 if (sync_state
== IP_VS_STATE_MASTER
)
736 sync_master_pid
= sync_pid
;
737 else if (sync_state
== IP_VS_STATE_BACKUP
)
738 sync_backup_pid
= sync_pid
;
741 static void set_stop_sync(int sync_state
, int set
)
743 if (sync_state
== IP_VS_STATE_MASTER
)
744 stop_master_sync
= set
;
745 else if (sync_state
== IP_VS_STATE_BACKUP
)
746 stop_backup_sync
= set
;
748 stop_master_sync
= set
;
749 stop_backup_sync
= set
;
753 static int sync_thread(void *startup
)
755 DECLARE_WAITQUEUE(wait
, current
);
759 struct ip_vs_sync_thread_data
*tinfo
= startup
;
761 /* increase the module use count */
762 ip_vs_use_count_inc();
764 if (ip_vs_sync_state
& IP_VS_STATE_MASTER
&& !sync_master_pid
) {
765 state
= IP_VS_STATE_MASTER
;
766 name
= "ipvs_syncmaster";
767 } else if (ip_vs_sync_state
& IP_VS_STATE_BACKUP
&& !sync_backup_pid
) {
768 state
= IP_VS_STATE_BACKUP
;
769 name
= "ipvs_syncbackup";
772 ip_vs_use_count_dec();
781 /* Block all signals */
782 spin_lock_irq(¤t
->sighand
->siglock
);
783 siginitsetinv(¤t
->blocked
, 0);
785 spin_unlock_irq(¤t
->sighand
->siglock
);
787 /* set the maximum length of sync message */
788 set_sync_mesg_maxlen(state
);
790 /* set up multicast address */
791 mcast_addr
.sin_family
= AF_INET
;
792 mcast_addr
.sin_port
= htons(IP_VS_SYNC_PORT
);
793 mcast_addr
.sin_addr
.s_addr
= htonl(IP_VS_SYNC_GROUP
);
795 add_wait_queue(&sync_wait
, &wait
);
797 set_sync_pid(state
, current
->pid
);
798 complete(tinfo
->startup
);
801 * once we call the completion queue above, we should
802 * null out that reference, since its allocated on the
803 * stack of the creating kernel thread
805 tinfo
->startup
= NULL
;
807 /* processing master/backup loop here */
808 if (state
== IP_VS_STATE_MASTER
)
810 else if (state
== IP_VS_STATE_BACKUP
)
814 remove_wait_queue(&sync_wait
, &wait
);
819 * If we weren't explicitly stopped, then we
820 * exited in error, and should undo our state
822 if ((!stop_master_sync
) && (!stop_backup_sync
))
823 ip_vs_sync_state
-= tinfo
->state
;
825 set_sync_pid(state
, 0);
826 IP_VS_INFO("sync thread stopped!\n");
830 /* decrease the module use count */
831 ip_vs_use_count_dec();
833 set_stop_sync(state
, 0);
834 wake_up(&stop_sync_wait
);
837 * we need to free the structure that was allocated
838 * for us in start_sync_thread
845 static int fork_sync_thread(void *startup
)
849 /* fork the sync thread here, then the parent process of the
850 sync thread is the init process after this thread exits. */
852 if ((pid
= kernel_thread(sync_thread
, startup
, 0)) < 0) {
853 IP_VS_ERR("could not create sync_thread due to %d... "
855 msleep_interruptible(1000);
863 int start_sync_thread(int state
, char *mcast_ifn
, __u8 syncid
)
865 DECLARE_COMPLETION_ONSTACK(startup
);
867 struct ip_vs_sync_thread_data
*tinfo
;
869 if ((state
== IP_VS_STATE_MASTER
&& sync_master_pid
) ||
870 (state
== IP_VS_STATE_BACKUP
&& sync_backup_pid
))
874 * Note that tinfo will be freed in sync_thread on exit
876 tinfo
= kmalloc(sizeof(struct ip_vs_sync_thread_data
), GFP_KERNEL
);
880 IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__
, task_pid_nr(current
));
881 IP_VS_DBG(7, "Each ip_vs_sync_conn entry need %Zd bytes\n",
882 sizeof(struct ip_vs_sync_conn
));
884 ip_vs_sync_state
|= state
;
885 if (state
== IP_VS_STATE_MASTER
) {
886 strlcpy(ip_vs_master_mcast_ifn
, mcast_ifn
,
887 sizeof(ip_vs_master_mcast_ifn
));
888 ip_vs_master_syncid
= syncid
;
890 strlcpy(ip_vs_backup_mcast_ifn
, mcast_ifn
,
891 sizeof(ip_vs_backup_mcast_ifn
));
892 ip_vs_backup_syncid
= syncid
;
895 tinfo
->state
= state
;
896 tinfo
->startup
= &startup
;
899 if ((pid
= kernel_thread(fork_sync_thread
, tinfo
, 0)) < 0) {
900 IP_VS_ERR("could not create fork_sync_thread due to %d... "
902 msleep_interruptible(1000);
906 wait_for_completion(&startup
);
912 int stop_sync_thread(int state
)
914 DECLARE_WAITQUEUE(wait
, current
);
916 if ((state
== IP_VS_STATE_MASTER
&& !sync_master_pid
) ||
917 (state
== IP_VS_STATE_BACKUP
&& !sync_backup_pid
))
920 IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__
, task_pid_nr(current
));
921 IP_VS_INFO("stopping sync thread %d ...\n",
922 (state
== IP_VS_STATE_MASTER
) ?
923 sync_master_pid
: sync_backup_pid
);
925 __set_current_state(TASK_UNINTERRUPTIBLE
);
926 add_wait_queue(&stop_sync_wait
, &wait
);
927 set_stop_sync(state
, 1);
928 ip_vs_sync_state
-= state
;
931 __set_current_state(TASK_RUNNING
);
932 remove_wait_queue(&stop_sync_wait
, &wait
);
934 /* Note: no need to reap the sync thread, because its parent
935 process is the init process */
937 if ((state
== IP_VS_STATE_MASTER
&& stop_master_sync
) ||
938 (state
== IP_VS_STATE_BACKUP
&& stop_backup_sync
))