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 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))
81 The master mulitcasts messages to the backup load balancers in the
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90 | IPVS Sync Connection (1) |
91 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97 | IPVS Sync Connection (n) |
98 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101 #define SYNC_MESG_HEADER_LEN 4
103 struct ip_vs_sync_mesg
{
108 /* ip_vs_sync_conn entries start here */
111 /* the maximum length of sync (sending/receiving) message */
112 static int sync_send_mesg_maxlen
;
113 static int sync_recv_mesg_maxlen
;
115 struct ip_vs_sync_buff
{
116 struct list_head list
;
117 unsigned long firstuse
;
119 /* pointers for the message data */
120 struct ip_vs_sync_mesg
*mesg
;
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
);
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
);
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;
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
];
144 static struct sockaddr_in mcast_addr
;
147 static inline void sb_queue_tail(struct ip_vs_sync_buff
*sb
)
149 spin_lock(&ip_vs_sync_lock
);
150 list_add_tail(&sb
->list
, &ip_vs_sync_queue
);
151 spin_unlock(&ip_vs_sync_lock
);
154 static inline struct ip_vs_sync_buff
* sb_dequeue(void)
156 struct ip_vs_sync_buff
*sb
;
158 spin_lock_bh(&ip_vs_sync_lock
);
159 if (list_empty(&ip_vs_sync_queue
)) {
162 sb
= list_entry(ip_vs_sync_queue
.next
,
163 struct ip_vs_sync_buff
,
167 spin_unlock_bh(&ip_vs_sync_lock
);
172 static inline struct ip_vs_sync_buff
* ip_vs_sync_buff_create(void)
174 struct ip_vs_sync_buff
*sb
;
176 if (!(sb
=kmalloc(sizeof(struct ip_vs_sync_buff
), GFP_ATOMIC
)))
179 if (!(sb
->mesg
=kmalloc(sync_send_mesg_maxlen
, GFP_ATOMIC
))) {
183 sb
->mesg
->nr_conns
= 0;
184 sb
->mesg
->syncid
= ip_vs_master_syncid
;
186 sb
->head
= (unsigned char *)sb
->mesg
+ 4;
187 sb
->end
= (unsigned char *)sb
->mesg
+ sync_send_mesg_maxlen
;
188 sb
->firstuse
= jiffies
;
192 static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff
*sb
)
199 * Get the current sync buffer if it has been created for more
200 * than the specified time or the specified time is zero.
202 static inline struct ip_vs_sync_buff
*
203 get_curr_sync_buff(unsigned long time
)
205 struct ip_vs_sync_buff
*sb
;
207 spin_lock_bh(&curr_sb_lock
);
208 if (curr_sb
&& (time
== 0 ||
209 time_before(jiffies
- curr_sb
->firstuse
, time
))) {
214 spin_unlock_bh(&curr_sb_lock
);
220 * Add an ip_vs_conn information into the current sync_buff.
221 * Called by ip_vs_in.
223 void ip_vs_sync_conn(struct ip_vs_conn
*cp
)
225 struct ip_vs_sync_mesg
*m
;
226 struct ip_vs_sync_conn
*s
;
229 spin_lock(&curr_sb_lock
);
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");
238 len
= (cp
->flags
& IP_VS_CONN_F_SEQ_MASK
) ? FULL_CONN_SIZE
:
241 s
= (struct ip_vs_sync_conn
*)curr_sb
->head
;
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
));
261 curr_sb
->head
+= len
;
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
);
268 spin_unlock(&curr_sb_lock
);
270 /* synchronize its controller if it has */
272 ip_vs_sync_conn(cp
->control
);
277 * Process received multicast message and create the corresponding
278 * ip_vs_conn entries.
280 static void ip_vs_process_message(const char *buffer
, const size_t buflen
)
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
;
291 if (buflen
< sizeof(struct ip_vs_sync_mesg
)) {
292 IP_VS_ERR_RL("sync message header too short\n");
296 /* Convert size back to host byte order */
297 m
->size
= ntohs(m
->size
);
299 if (buflen
!= m
->size
) {
300 IP_VS_ERR_RL("bogus sync message size\n");
304 /* SyncID sanity check */
305 if (ip_vs_backup_syncid
!= 0 && m
->syncid
!= ip_vs_backup_syncid
) {
306 IP_VS_DBG(7, "Ignoring incoming msg with syncid = %d\n",
311 p
= (char *)buffer
+ sizeof(struct ip_vs_sync_mesg
);
312 for (i
=0; i
<m
->nr_conns
; i
++) {
313 unsigned flags
, state
;
315 if (p
+ SIMPLE_CONN_SIZE
> buffer
+buflen
) {
316 IP_VS_ERR_RL("bogus conn in sync message\n");
319 s
= (struct ip_vs_sync_conn
*) p
;
320 flags
= ntohs(s
->flags
) | IP_VS_CONN_F_SYNC
;
321 flags
&= ~IP_VS_CONN_F_HASHED
;
322 if (flags
& IP_VS_CONN_F_SEQ_MASK
) {
323 opt
= (struct ip_vs_sync_conn_options
*)&s
[1];
325 if (p
> buffer
+buflen
) {
326 IP_VS_ERR_RL("bogus conn options in sync message\n");
331 p
+= SIMPLE_CONN_SIZE
;
334 state
= ntohs(s
->state
);
335 if (!(flags
& IP_VS_CONN_F_TEMPLATE
)) {
336 pp
= ip_vs_proto_get(s
->protocol
);
338 IP_VS_ERR_RL("Unsupported protocol %u in sync msg\n",
342 if (state
>= pp
->num_states
) {
343 IP_VS_DBG(2, "Invalid %s state %u in sync msg\n",
348 /* protocol in templates is not used for state/timeout */
351 IP_VS_DBG(2, "Invalid template state %u in sync msg\n",
357 if (!(flags
& IP_VS_CONN_F_TEMPLATE
))
358 cp
= ip_vs_conn_in_get(s
->protocol
,
362 cp
= ip_vs_ct_in_get(s
->protocol
,
367 * Find the appropriate destination for the connection.
368 * If it is not found the connection will remain unbound
371 dest
= ip_vs_find_dest(s
->daddr
, s
->dport
,
374 /* Set the approprite ativity flag */
375 if (s
->protocol
== IPPROTO_TCP
) {
376 if (state
!= IP_VS_TCP_S_ESTABLISHED
)
377 flags
|= IP_VS_CONN_F_INACTIVE
;
379 flags
&= ~IP_VS_CONN_F_INACTIVE
;
381 cp
= ip_vs_conn_new(s
->protocol
,
387 atomic_dec(&dest
->refcnt
);
389 IP_VS_ERR("ip_vs_conn_new failed\n");
392 } else if (!cp
->dest
) {
393 dest
= ip_vs_try_bind_dest(cp
);
395 atomic_dec(&dest
->refcnt
);
396 } else if ((cp
->dest
) && (cp
->protocol
== IPPROTO_TCP
) &&
397 (cp
->state
!= state
)) {
398 /* update active/inactive flag for the connection */
400 if (!(cp
->flags
& IP_VS_CONN_F_INACTIVE
) &&
401 (state
!= IP_VS_TCP_S_ESTABLISHED
)) {
402 atomic_dec(&dest
->activeconns
);
403 atomic_inc(&dest
->inactconns
);
404 cp
->flags
|= IP_VS_CONN_F_INACTIVE
;
405 } else if ((cp
->flags
& IP_VS_CONN_F_INACTIVE
) &&
406 (state
== IP_VS_TCP_S_ESTABLISHED
)) {
407 atomic_inc(&dest
->activeconns
);
408 atomic_dec(&dest
->inactconns
);
409 cp
->flags
&= ~IP_VS_CONN_F_INACTIVE
;
414 memcpy(&cp
->in_seq
, opt
, sizeof(*opt
));
415 atomic_set(&cp
->in_pkts
, sysctl_ip_vs_sync_threshold
[0]);
417 cp
->old_state
= cp
->state
;
419 * We can not recover the right timeout for templates
420 * in all cases, we can not find the right fwmark
421 * virtual service. If needed, we can do it for
422 * non-fwmark persistent services.
424 if (!(flags
& IP_VS_CONN_F_TEMPLATE
) && pp
->timeout_table
)
425 cp
->timeout
= pp
->timeout_table
[state
];
427 cp
->timeout
= (3*60*HZ
);
434 * Setup loopback of outgoing multicasts on a sending socket
436 static void set_mcast_loop(struct sock
*sk
, u_char loop
)
438 struct inet_sock
*inet
= inet_sk(sk
);
440 /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
442 inet
->mc_loop
= loop
? 1 : 0;
447 * Specify TTL for outgoing multicasts on a sending socket
449 static void set_mcast_ttl(struct sock
*sk
, u_char ttl
)
451 struct inet_sock
*inet
= inet_sk(sk
);
453 /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
460 * Specifiy default interface for outgoing multicasts
462 static int set_mcast_if(struct sock
*sk
, char *ifname
)
464 struct net_device
*dev
;
465 struct inet_sock
*inet
= inet_sk(sk
);
467 if ((dev
= __dev_get_by_name(&init_net
, ifname
)) == NULL
)
470 if (sk
->sk_bound_dev_if
&& dev
->ifindex
!= sk
->sk_bound_dev_if
)
474 inet
->mc_index
= dev
->ifindex
;
475 /* inet->mc_addr = 0; */
483 * Set the maximum length of sync message according to the
484 * specified interface's MTU.
486 static int set_sync_mesg_maxlen(int sync_state
)
488 struct net_device
*dev
;
491 if (sync_state
== IP_VS_STATE_MASTER
) {
492 if ((dev
= __dev_get_by_name(&init_net
, ip_vs_master_mcast_ifn
)) == NULL
)
495 num
= (dev
->mtu
- sizeof(struct iphdr
) -
496 sizeof(struct udphdr
) -
497 SYNC_MESG_HEADER_LEN
- 20) / SIMPLE_CONN_SIZE
;
498 sync_send_mesg_maxlen
=
499 SYNC_MESG_HEADER_LEN
+ SIMPLE_CONN_SIZE
* num
;
500 IP_VS_DBG(7, "setting the maximum length of sync sending "
501 "message %d.\n", sync_send_mesg_maxlen
);
502 } else if (sync_state
== IP_VS_STATE_BACKUP
) {
503 if ((dev
= __dev_get_by_name(&init_net
, ip_vs_backup_mcast_ifn
)) == NULL
)
506 sync_recv_mesg_maxlen
= dev
->mtu
-
507 sizeof(struct iphdr
) - sizeof(struct udphdr
);
508 IP_VS_DBG(7, "setting the maximum length of sync receiving "
509 "message %d.\n", sync_recv_mesg_maxlen
);
517 * Join a multicast group.
518 * the group is specified by a class D multicast address 224.0.0.0/8
519 * in the in_addr structure passed in as a parameter.
522 join_mcast_group(struct sock
*sk
, struct in_addr
*addr
, char *ifname
)
524 struct ip_mreqn mreq
;
525 struct net_device
*dev
;
528 memset(&mreq
, 0, sizeof(mreq
));
529 memcpy(&mreq
.imr_multiaddr
, addr
, sizeof(struct in_addr
));
531 if ((dev
= __dev_get_by_name(&init_net
, ifname
)) == NULL
)
533 if (sk
->sk_bound_dev_if
&& dev
->ifindex
!= sk
->sk_bound_dev_if
)
536 mreq
.imr_ifindex
= dev
->ifindex
;
539 ret
= ip_mc_join_group(sk
, &mreq
);
546 static int bind_mcastif_addr(struct socket
*sock
, char *ifname
)
548 struct net_device
*dev
;
550 struct sockaddr_in sin
;
552 if ((dev
= __dev_get_by_name(&init_net
, ifname
)) == NULL
)
555 addr
= inet_select_addr(dev
, 0, RT_SCOPE_UNIVERSE
);
557 IP_VS_ERR("You probably need to specify IP address on "
558 "multicast interface.\n");
560 IP_VS_DBG(7, "binding socket with (%s) %u.%u.%u.%u\n",
561 ifname
, NIPQUAD(addr
));
563 /* Now bind the socket with the address of multicast interface */
564 sin
.sin_family
= AF_INET
;
565 sin
.sin_addr
.s_addr
= addr
;
568 return sock
->ops
->bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
));
572 * Set up sending multicast socket over UDP
574 static struct socket
* make_send_sock(void)
578 /* First create a socket */
579 if (sock_create_kern(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
) < 0) {
580 IP_VS_ERR("Error during creation of socket; terminating\n");
584 if (set_mcast_if(sock
->sk
, ip_vs_master_mcast_ifn
) < 0) {
585 IP_VS_ERR("Error setting outbound mcast interface\n");
589 set_mcast_loop(sock
->sk
, 0);
590 set_mcast_ttl(sock
->sk
, 1);
592 if (bind_mcastif_addr(sock
, ip_vs_master_mcast_ifn
) < 0) {
593 IP_VS_ERR("Error binding address of the mcast interface\n");
597 if (sock
->ops
->connect(sock
,
598 (struct sockaddr
*)&mcast_addr
,
599 sizeof(struct sockaddr
), 0) < 0) {
600 IP_VS_ERR("Error connecting to the multicast addr\n");
613 * Set up receiving multicast socket over UDP
615 static struct socket
* make_receive_sock(void)
619 /* First create a socket */
620 if (sock_create_kern(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
) < 0) {
621 IP_VS_ERR("Error during creation of socket; terminating\n");
625 /* it is equivalent to the REUSEADDR option in user-space */
626 sock
->sk
->sk_reuse
= 1;
628 if (sock
->ops
->bind(sock
,
629 (struct sockaddr
*)&mcast_addr
,
630 sizeof(struct sockaddr
)) < 0) {
631 IP_VS_ERR("Error binding to the multicast addr\n");
635 /* join the multicast group */
636 if (join_mcast_group(sock
->sk
,
637 (struct in_addr
*)&mcast_addr
.sin_addr
,
638 ip_vs_backup_mcast_ifn
) < 0) {
639 IP_VS_ERR("Error joining to the multicast group\n");
652 ip_vs_send_async(struct socket
*sock
, const char *buffer
, const size_t length
)
654 struct msghdr msg
= {.msg_flags
= MSG_DONTWAIT
|MSG_NOSIGNAL
};
659 iov
.iov_base
= (void *)buffer
;
660 iov
.iov_len
= length
;
662 len
= kernel_sendmsg(sock
, &msg
, &iov
, 1, (size_t)(length
));
669 ip_vs_send_sync_msg(struct socket
*sock
, struct ip_vs_sync_mesg
*msg
)
675 /* Put size in network byte order */
676 msg
->size
= htons(msg
->size
);
678 if (ip_vs_send_async(sock
, (char *)msg
, msize
) != msize
)
679 IP_VS_ERR("ip_vs_send_async error\n");
683 ip_vs_receive(struct socket
*sock
, char *buffer
, const size_t buflen
)
685 struct msghdr msg
= {NULL
,};
691 /* Receive a packet */
692 iov
.iov_base
= buffer
;
693 iov
.iov_len
= (size_t)buflen
;
695 len
= kernel_recvmsg(sock
, &msg
, &iov
, 1, buflen
, 0);
705 static DECLARE_WAIT_QUEUE_HEAD(sync_wait
);
706 static pid_t sync_master_pid
= 0;
707 static pid_t sync_backup_pid
= 0;
709 static DECLARE_WAIT_QUEUE_HEAD(stop_sync_wait
);
710 static int stop_master_sync
= 0;
711 static int stop_backup_sync
= 0;
713 static void sync_master_loop(void)
716 struct ip_vs_sync_buff
*sb
;
718 /* create the sending multicast socket */
719 sock
= make_send_sock();
723 IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, "
725 ip_vs_master_mcast_ifn
, ip_vs_master_syncid
);
728 while ((sb
=sb_dequeue())) {
729 ip_vs_send_sync_msg(sock
, sb
->mesg
);
730 ip_vs_sync_buff_release(sb
);
733 /* check if entries stay in curr_sb for 2 seconds */
734 if ((sb
= get_curr_sync_buff(2*HZ
))) {
735 ip_vs_send_sync_msg(sock
, sb
->mesg
);
736 ip_vs_sync_buff_release(sb
);
739 if (stop_master_sync
)
742 msleep_interruptible(1000);
745 /* clean up the sync_buff queue */
746 while ((sb
=sb_dequeue())) {
747 ip_vs_sync_buff_release(sb
);
750 /* clean up the current sync_buff */
751 if ((sb
= get_curr_sync_buff(0))) {
752 ip_vs_sync_buff_release(sb
);
755 /* release the sending multicast socket */
760 static void sync_backup_loop(void)
766 if (!(buf
= kmalloc(sync_recv_mesg_maxlen
, GFP_ATOMIC
))) {
767 IP_VS_ERR("sync_backup_loop: kmalloc error\n");
771 /* create the receiving multicast socket */
772 sock
= make_receive_sock();
776 IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
778 ip_vs_backup_mcast_ifn
, ip_vs_backup_syncid
);
781 /* do you have data now? */
782 while (!skb_queue_empty(&(sock
->sk
->sk_receive_queue
))) {
784 ip_vs_receive(sock
, buf
,
785 sync_recv_mesg_maxlen
)) <= 0) {
786 IP_VS_ERR("receiving message error\n");
789 /* disable bottom half, because it accessed the data
790 shared by softirq while getting/creating conns */
792 ip_vs_process_message(buf
, len
);
796 if (stop_backup_sync
)
799 msleep_interruptible(1000);
802 /* release the sending multicast socket */
810 static void set_sync_pid(int sync_state
, pid_t sync_pid
)
812 if (sync_state
== IP_VS_STATE_MASTER
)
813 sync_master_pid
= sync_pid
;
814 else if (sync_state
== IP_VS_STATE_BACKUP
)
815 sync_backup_pid
= sync_pid
;
818 static void set_stop_sync(int sync_state
, int set
)
820 if (sync_state
== IP_VS_STATE_MASTER
)
821 stop_master_sync
= set
;
822 else if (sync_state
== IP_VS_STATE_BACKUP
)
823 stop_backup_sync
= set
;
825 stop_master_sync
= set
;
826 stop_backup_sync
= set
;
830 static int sync_thread(void *startup
)
832 DECLARE_WAITQUEUE(wait
, current
);
836 struct ip_vs_sync_thread_data
*tinfo
= startup
;
838 /* increase the module use count */
839 ip_vs_use_count_inc();
841 if (ip_vs_sync_state
& IP_VS_STATE_MASTER
&& !sync_master_pid
) {
842 state
= IP_VS_STATE_MASTER
;
843 name
= "ipvs_syncmaster";
844 } else if (ip_vs_sync_state
& IP_VS_STATE_BACKUP
&& !sync_backup_pid
) {
845 state
= IP_VS_STATE_BACKUP
;
846 name
= "ipvs_syncbackup";
849 ip_vs_use_count_dec();
858 /* Block all signals */
859 spin_lock_irq(¤t
->sighand
->siglock
);
860 siginitsetinv(¤t
->blocked
, 0);
862 spin_unlock_irq(¤t
->sighand
->siglock
);
864 /* set the maximum length of sync message */
865 set_sync_mesg_maxlen(state
);
867 /* set up multicast address */
868 mcast_addr
.sin_family
= AF_INET
;
869 mcast_addr
.sin_port
= htons(IP_VS_SYNC_PORT
);
870 mcast_addr
.sin_addr
.s_addr
= htonl(IP_VS_SYNC_GROUP
);
872 add_wait_queue(&sync_wait
, &wait
);
874 set_sync_pid(state
, task_pid_nr(current
));
875 complete(tinfo
->startup
);
878 * once we call the completion queue above, we should
879 * null out that reference, since its allocated on the
880 * stack of the creating kernel thread
882 tinfo
->startup
= NULL
;
884 /* processing master/backup loop here */
885 if (state
== IP_VS_STATE_MASTER
)
887 else if (state
== IP_VS_STATE_BACKUP
)
891 remove_wait_queue(&sync_wait
, &wait
);
896 * If we weren't explicitly stopped, then we
897 * exited in error, and should undo our state
899 if ((!stop_master_sync
) && (!stop_backup_sync
))
900 ip_vs_sync_state
-= tinfo
->state
;
902 set_sync_pid(state
, 0);
903 IP_VS_INFO("sync thread stopped!\n");
907 /* decrease the module use count */
908 ip_vs_use_count_dec();
910 set_stop_sync(state
, 0);
911 wake_up(&stop_sync_wait
);
914 * we need to free the structure that was allocated
915 * for us in start_sync_thread
922 static int fork_sync_thread(void *startup
)
926 /* fork the sync thread here, then the parent process of the
927 sync thread is the init process after this thread exits. */
929 if ((pid
= kernel_thread(sync_thread
, startup
, 0)) < 0) {
930 IP_VS_ERR("could not create sync_thread due to %d... "
932 msleep_interruptible(1000);
940 int start_sync_thread(int state
, char *mcast_ifn
, __u8 syncid
)
942 DECLARE_COMPLETION_ONSTACK(startup
);
944 struct ip_vs_sync_thread_data
*tinfo
;
946 if ((state
== IP_VS_STATE_MASTER
&& sync_master_pid
) ||
947 (state
== IP_VS_STATE_BACKUP
&& sync_backup_pid
))
951 * Note that tinfo will be freed in sync_thread on exit
953 tinfo
= kmalloc(sizeof(struct ip_vs_sync_thread_data
), GFP_KERNEL
);
957 IP_VS_DBG(7, "%s: pid %d\n", __func__
, task_pid_nr(current
));
958 IP_VS_DBG(7, "Each ip_vs_sync_conn entry need %Zd bytes\n",
959 sizeof(struct ip_vs_sync_conn
));
961 ip_vs_sync_state
|= state
;
962 if (state
== IP_VS_STATE_MASTER
) {
963 strlcpy(ip_vs_master_mcast_ifn
, mcast_ifn
,
964 sizeof(ip_vs_master_mcast_ifn
));
965 ip_vs_master_syncid
= syncid
;
967 strlcpy(ip_vs_backup_mcast_ifn
, mcast_ifn
,
968 sizeof(ip_vs_backup_mcast_ifn
));
969 ip_vs_backup_syncid
= syncid
;
972 tinfo
->state
= state
;
973 tinfo
->startup
= &startup
;
976 if ((pid
= kernel_thread(fork_sync_thread
, tinfo
, 0)) < 0) {
977 IP_VS_ERR("could not create fork_sync_thread due to %d... "
979 msleep_interruptible(1000);
983 wait_for_completion(&startup
);
989 int stop_sync_thread(int state
)
991 DECLARE_WAITQUEUE(wait
, current
);
993 if ((state
== IP_VS_STATE_MASTER
&& !sync_master_pid
) ||
994 (state
== IP_VS_STATE_BACKUP
&& !sync_backup_pid
))
997 IP_VS_DBG(7, "%s: pid %d\n", __func__
, task_pid_nr(current
));
998 IP_VS_INFO("stopping sync thread %d ...\n",
999 (state
== IP_VS_STATE_MASTER
) ?
1000 sync_master_pid
: sync_backup_pid
);
1002 __set_current_state(TASK_UNINTERRUPTIBLE
);
1003 add_wait_queue(&stop_sync_wait
, &wait
);
1004 set_stop_sync(state
, 1);
1005 ip_vs_sync_state
-= state
;
1006 wake_up(&sync_wait
);
1008 __set_current_state(TASK_RUNNING
);
1009 remove_wait_queue(&stop_sync_wait
, &wait
);
1011 /* Note: no need to reap the sync thread, because its parent
1012 process is the init process */
1014 if ((state
== IP_VS_STATE_MASTER
&& stop_master_sync
) ||
1015 (state
== IP_VS_STATE_BACKUP
&& stop_backup_sync
))