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>
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
{
47 /* Protocol, addresses and port numbers */
48 __u8 protocol
; /* Which protocol (TCP/UDP) */
52 __be32 caddr
; /* client address */
53 __be32 vaddr
; /* virtual address */
54 __be32 daddr
; /* destination address */
56 /* Flags and state transition */
57 __be16 flags
; /* status flags */
58 __be16 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 struct ip_vs_sync_thread_data
{
69 struct completion
*startup
;
73 #define SIMPLE_CONN_SIZE (sizeof(struct ip_vs_sync_conn))
74 #define FULL_CONN_SIZE \
75 (sizeof(struct ip_vs_sync_conn) + sizeof(struct ip_vs_sync_conn_options))
79 The master mulitcasts messages to the backup load balancers in the
83 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
84 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
85 | Count Conns | SyncID | Size |
86 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
88 | IPVS Sync Connection (1) |
89 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95 | IPVS Sync Connection (n) |
96 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99 #define SYNC_MESG_HEADER_LEN 4
101 struct ip_vs_sync_mesg
{
106 /* ip_vs_sync_conn entries start here */
109 /* the maximum length of sync (sending/receiving) message */
110 static int sync_send_mesg_maxlen
;
111 static int sync_recv_mesg_maxlen
;
113 struct ip_vs_sync_buff
{
114 struct list_head list
;
115 unsigned long firstuse
;
117 /* pointers for the message data */
118 struct ip_vs_sync_mesg
*mesg
;
124 /* the sync_buff list head and the lock */
125 static LIST_HEAD(ip_vs_sync_queue
);
126 static DEFINE_SPINLOCK(ip_vs_sync_lock
);
128 /* current sync_buff for accepting new conn entries */
129 static struct ip_vs_sync_buff
*curr_sb
= NULL
;
130 static DEFINE_SPINLOCK(curr_sb_lock
);
132 /* ipvs sync daemon state */
133 volatile int ip_vs_sync_state
= IP_VS_STATE_NONE
;
134 volatile int ip_vs_master_syncid
= 0;
135 volatile int ip_vs_backup_syncid
= 0;
137 /* multicast interface name */
138 char ip_vs_master_mcast_ifn
[IP_VS_IFNAME_MAXLEN
];
139 char ip_vs_backup_mcast_ifn
[IP_VS_IFNAME_MAXLEN
];
142 static struct sockaddr_in mcast_addr
;
145 static inline void sb_queue_tail(struct ip_vs_sync_buff
*sb
)
147 spin_lock(&ip_vs_sync_lock
);
148 list_add_tail(&sb
->list
, &ip_vs_sync_queue
);
149 spin_unlock(&ip_vs_sync_lock
);
152 static inline struct ip_vs_sync_buff
* sb_dequeue(void)
154 struct ip_vs_sync_buff
*sb
;
156 spin_lock_bh(&ip_vs_sync_lock
);
157 if (list_empty(&ip_vs_sync_queue
)) {
160 sb
= list_entry(ip_vs_sync_queue
.next
,
161 struct ip_vs_sync_buff
,
165 spin_unlock_bh(&ip_vs_sync_lock
);
170 static inline struct ip_vs_sync_buff
* ip_vs_sync_buff_create(void)
172 struct ip_vs_sync_buff
*sb
;
174 if (!(sb
=kmalloc(sizeof(struct ip_vs_sync_buff
), GFP_ATOMIC
)))
177 if (!(sb
->mesg
=kmalloc(sync_send_mesg_maxlen
, GFP_ATOMIC
))) {
181 sb
->mesg
->nr_conns
= 0;
182 sb
->mesg
->syncid
= ip_vs_master_syncid
;
184 sb
->head
= (unsigned char *)sb
->mesg
+ 4;
185 sb
->end
= (unsigned char *)sb
->mesg
+ sync_send_mesg_maxlen
;
186 sb
->firstuse
= jiffies
;
190 static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff
*sb
)
197 * Get the current sync buffer if it has been created for more
198 * than the specified time or the specified time is zero.
200 static inline struct ip_vs_sync_buff
*
201 get_curr_sync_buff(unsigned long time
)
203 struct ip_vs_sync_buff
*sb
;
205 spin_lock_bh(&curr_sb_lock
);
206 if (curr_sb
&& (time
== 0 ||
207 time_before(jiffies
- curr_sb
->firstuse
, time
))) {
212 spin_unlock_bh(&curr_sb_lock
);
218 * Add an ip_vs_conn information into the current sync_buff.
219 * Called by ip_vs_in.
221 void ip_vs_sync_conn(struct ip_vs_conn
*cp
)
223 struct ip_vs_sync_mesg
*m
;
224 struct ip_vs_sync_conn
*s
;
227 spin_lock(&curr_sb_lock
);
229 if (!(curr_sb
=ip_vs_sync_buff_create())) {
230 spin_unlock(&curr_sb_lock
);
231 IP_VS_ERR("ip_vs_sync_buff_create failed.\n");
236 len
= (cp
->flags
& IP_VS_CONN_F_SEQ_MASK
) ? FULL_CONN_SIZE
:
239 s
= (struct ip_vs_sync_conn
*)curr_sb
->head
;
242 s
->protocol
= cp
->protocol
;
243 s
->cport
= cp
->cport
;
244 s
->vport
= cp
->vport
;
245 s
->dport
= cp
->dport
;
246 s
->caddr
= cp
->caddr
;
247 s
->vaddr
= cp
->vaddr
;
248 s
->daddr
= cp
->daddr
;
249 s
->flags
= htons(cp
->flags
& ~IP_VS_CONN_F_HASHED
);
250 s
->state
= htons(cp
->state
);
251 if (cp
->flags
& IP_VS_CONN_F_SEQ_MASK
) {
252 struct ip_vs_sync_conn_options
*opt
=
253 (struct ip_vs_sync_conn_options
*)&s
[1];
254 memcpy(opt
, &cp
->in_seq
, sizeof(*opt
));
259 curr_sb
->head
+= len
;
261 /* check if there is a space for next one */
262 if (curr_sb
->head
+FULL_CONN_SIZE
> curr_sb
->end
) {
263 sb_queue_tail(curr_sb
);
266 spin_unlock(&curr_sb_lock
);
268 /* synchronize its controller if it has */
270 ip_vs_sync_conn(cp
->control
);
275 * Process received multicast message and create the corresponding
276 * ip_vs_conn entries.
278 static void ip_vs_process_message(const char *buffer
, const size_t buflen
)
280 struct ip_vs_sync_mesg
*m
= (struct ip_vs_sync_mesg
*)buffer
;
281 struct ip_vs_sync_conn
*s
;
282 struct ip_vs_sync_conn_options
*opt
;
283 struct ip_vs_conn
*cp
;
284 struct ip_vs_protocol
*pp
;
285 struct ip_vs_dest
*dest
;
289 if (buflen
< sizeof(struct ip_vs_sync_mesg
)) {
290 IP_VS_ERR_RL("sync message header too short\n");
294 /* Convert size back to host byte order */
295 m
->size
= ntohs(m
->size
);
297 if (buflen
!= m
->size
) {
298 IP_VS_ERR_RL("bogus sync message size\n");
302 /* SyncID sanity check */
303 if (ip_vs_backup_syncid
!= 0 && m
->syncid
!= ip_vs_backup_syncid
) {
304 IP_VS_DBG(7, "Ignoring incoming msg with syncid = %d\n",
309 p
= (char *)buffer
+ sizeof(struct ip_vs_sync_mesg
);
310 for (i
=0; i
<m
->nr_conns
; i
++) {
311 unsigned flags
, state
;
313 if (p
+ SIMPLE_CONN_SIZE
> buffer
+buflen
) {
314 IP_VS_ERR_RL("bogus conn in sync message\n");
317 s
= (struct ip_vs_sync_conn
*) p
;
318 flags
= ntohs(s
->flags
) | IP_VS_CONN_F_SYNC
;
319 flags
&= ~IP_VS_CONN_F_HASHED
;
320 if (flags
& IP_VS_CONN_F_SEQ_MASK
) {
321 opt
= (struct ip_vs_sync_conn_options
*)&s
[1];
323 if (p
> buffer
+buflen
) {
324 IP_VS_ERR_RL("bogus conn options in sync message\n");
329 p
+= SIMPLE_CONN_SIZE
;
332 state
= ntohs(s
->state
);
333 if (!(flags
& IP_VS_CONN_F_TEMPLATE
)) {
334 pp
= ip_vs_proto_get(s
->protocol
);
336 IP_VS_ERR_RL("Unsupported protocol %u in sync msg\n",
340 if (state
>= pp
->num_states
) {
341 IP_VS_DBG(2, "Invalid %s state %u in sync msg\n",
346 /* protocol in templates is not used for state/timeout */
349 IP_VS_DBG(2, "Invalid template state %u in sync msg\n",
355 if (!(flags
& IP_VS_CONN_F_TEMPLATE
))
356 cp
= ip_vs_conn_in_get(s
->protocol
,
360 cp
= ip_vs_ct_in_get(s
->protocol
,
365 * Find the appropriate destination for the connection.
366 * If it is not found the connection will remain unbound
369 dest
= ip_vs_find_dest(s
->daddr
, s
->dport
,
372 /* Set the approprite ativity flag */
373 if (s
->protocol
== IPPROTO_TCP
) {
374 if (state
!= IP_VS_TCP_S_ESTABLISHED
)
375 flags
|= IP_VS_CONN_F_INACTIVE
;
377 flags
&= ~IP_VS_CONN_F_INACTIVE
;
379 cp
= ip_vs_conn_new(s
->protocol
,
385 atomic_dec(&dest
->refcnt
);
387 IP_VS_ERR("ip_vs_conn_new failed\n");
390 } else if (!cp
->dest
) {
391 dest
= ip_vs_try_bind_dest(cp
);
393 atomic_dec(&dest
->refcnt
);
394 } else if ((cp
->dest
) && (cp
->protocol
== IPPROTO_TCP
) &&
395 (cp
->state
!= state
)) {
396 /* update active/inactive flag for the connection */
398 if (!(cp
->flags
& IP_VS_CONN_F_INACTIVE
) &&
399 (state
!= IP_VS_TCP_S_ESTABLISHED
)) {
400 atomic_dec(&dest
->activeconns
);
401 atomic_inc(&dest
->inactconns
);
402 cp
->flags
|= IP_VS_CONN_F_INACTIVE
;
403 } else if ((cp
->flags
& IP_VS_CONN_F_INACTIVE
) &&
404 (state
== IP_VS_TCP_S_ESTABLISHED
)) {
405 atomic_inc(&dest
->activeconns
);
406 atomic_dec(&dest
->inactconns
);
407 cp
->flags
&= ~IP_VS_CONN_F_INACTIVE
;
412 memcpy(&cp
->in_seq
, opt
, sizeof(*opt
));
413 atomic_set(&cp
->in_pkts
, sysctl_ip_vs_sync_threshold
[0]);
415 cp
->old_state
= cp
->state
;
417 * We can not recover the right timeout for templates
418 * in all cases, we can not find the right fwmark
419 * virtual service. If needed, we can do it for
420 * non-fwmark persistent services.
422 if (!(flags
& IP_VS_CONN_F_TEMPLATE
) && pp
->timeout_table
)
423 cp
->timeout
= pp
->timeout_table
[state
];
425 cp
->timeout
= (3*60*HZ
);
432 * Setup loopback of outgoing multicasts on a sending socket
434 static void set_mcast_loop(struct sock
*sk
, u_char loop
)
436 struct inet_sock
*inet
= inet_sk(sk
);
438 /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
440 inet
->mc_loop
= loop
? 1 : 0;
445 * Specify TTL for outgoing multicasts on a sending socket
447 static void set_mcast_ttl(struct sock
*sk
, u_char ttl
)
449 struct inet_sock
*inet
= inet_sk(sk
);
451 /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
458 * Specifiy default interface for outgoing multicasts
460 static int set_mcast_if(struct sock
*sk
, char *ifname
)
462 struct net_device
*dev
;
463 struct inet_sock
*inet
= inet_sk(sk
);
465 if ((dev
= __dev_get_by_name(&init_net
, ifname
)) == NULL
)
468 if (sk
->sk_bound_dev_if
&& dev
->ifindex
!= sk
->sk_bound_dev_if
)
472 inet
->mc_index
= dev
->ifindex
;
473 /* inet->mc_addr = 0; */
481 * Set the maximum length of sync message according to the
482 * specified interface's MTU.
484 static int set_sync_mesg_maxlen(int sync_state
)
486 struct net_device
*dev
;
489 if (sync_state
== IP_VS_STATE_MASTER
) {
490 if ((dev
= __dev_get_by_name(&init_net
, ip_vs_master_mcast_ifn
)) == NULL
)
493 num
= (dev
->mtu
- sizeof(struct iphdr
) -
494 sizeof(struct udphdr
) -
495 SYNC_MESG_HEADER_LEN
- 20) / SIMPLE_CONN_SIZE
;
496 sync_send_mesg_maxlen
=
497 SYNC_MESG_HEADER_LEN
+ SIMPLE_CONN_SIZE
* num
;
498 IP_VS_DBG(7, "setting the maximum length of sync sending "
499 "message %d.\n", sync_send_mesg_maxlen
);
500 } else if (sync_state
== IP_VS_STATE_BACKUP
) {
501 if ((dev
= __dev_get_by_name(&init_net
, ip_vs_backup_mcast_ifn
)) == NULL
)
504 sync_recv_mesg_maxlen
= dev
->mtu
-
505 sizeof(struct iphdr
) - sizeof(struct udphdr
);
506 IP_VS_DBG(7, "setting the maximum length of sync receiving "
507 "message %d.\n", sync_recv_mesg_maxlen
);
515 * Join a multicast group.
516 * the group is specified by a class D multicast address 224.0.0.0/8
517 * in the in_addr structure passed in as a parameter.
520 join_mcast_group(struct sock
*sk
, struct in_addr
*addr
, char *ifname
)
522 struct ip_mreqn mreq
;
523 struct net_device
*dev
;
526 memset(&mreq
, 0, sizeof(mreq
));
527 memcpy(&mreq
.imr_multiaddr
, addr
, sizeof(struct in_addr
));
529 if ((dev
= __dev_get_by_name(&init_net
, ifname
)) == NULL
)
531 if (sk
->sk_bound_dev_if
&& dev
->ifindex
!= sk
->sk_bound_dev_if
)
534 mreq
.imr_ifindex
= dev
->ifindex
;
537 ret
= ip_mc_join_group(sk
, &mreq
);
544 static int bind_mcastif_addr(struct socket
*sock
, char *ifname
)
546 struct net_device
*dev
;
548 struct sockaddr_in sin
;
550 if ((dev
= __dev_get_by_name(&init_net
, ifname
)) == NULL
)
553 addr
= inet_select_addr(dev
, 0, RT_SCOPE_UNIVERSE
);
555 IP_VS_ERR("You probably need to specify IP address on "
556 "multicast interface.\n");
558 IP_VS_DBG(7, "binding socket with (%s) %u.%u.%u.%u\n",
559 ifname
, NIPQUAD(addr
));
561 /* Now bind the socket with the address of multicast interface */
562 sin
.sin_family
= AF_INET
;
563 sin
.sin_addr
.s_addr
= addr
;
566 return sock
->ops
->bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
));
570 * Set up sending multicast socket over UDP
572 static struct socket
* make_send_sock(void)
576 /* First create a socket */
577 if (sock_create_kern(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
) < 0) {
578 IP_VS_ERR("Error during creation of socket; terminating\n");
582 if (set_mcast_if(sock
->sk
, ip_vs_master_mcast_ifn
) < 0) {
583 IP_VS_ERR("Error setting outbound mcast interface\n");
587 set_mcast_loop(sock
->sk
, 0);
588 set_mcast_ttl(sock
->sk
, 1);
590 if (bind_mcastif_addr(sock
, ip_vs_master_mcast_ifn
) < 0) {
591 IP_VS_ERR("Error binding address of the mcast interface\n");
595 if (sock
->ops
->connect(sock
,
596 (struct sockaddr
*)&mcast_addr
,
597 sizeof(struct sockaddr
), 0) < 0) {
598 IP_VS_ERR("Error connecting to the multicast addr\n");
611 * Set up receiving multicast socket over UDP
613 static struct socket
* make_receive_sock(void)
617 /* First create a socket */
618 if (sock_create_kern(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
) < 0) {
619 IP_VS_ERR("Error during creation of socket; terminating\n");
623 /* it is equivalent to the REUSEADDR option in user-space */
624 sock
->sk
->sk_reuse
= 1;
626 if (sock
->ops
->bind(sock
,
627 (struct sockaddr
*)&mcast_addr
,
628 sizeof(struct sockaddr
)) < 0) {
629 IP_VS_ERR("Error binding to the multicast addr\n");
633 /* join the multicast group */
634 if (join_mcast_group(sock
->sk
,
635 (struct in_addr
*)&mcast_addr
.sin_addr
,
636 ip_vs_backup_mcast_ifn
) < 0) {
637 IP_VS_ERR("Error joining to the multicast group\n");
650 ip_vs_send_async(struct socket
*sock
, const char *buffer
, const size_t length
)
652 struct msghdr msg
= {.msg_flags
= MSG_DONTWAIT
|MSG_NOSIGNAL
};
657 iov
.iov_base
= (void *)buffer
;
658 iov
.iov_len
= length
;
660 len
= kernel_sendmsg(sock
, &msg
, &iov
, 1, (size_t)(length
));
667 ip_vs_send_sync_msg(struct socket
*sock
, struct ip_vs_sync_mesg
*msg
)
673 /* Put size in network byte order */
674 msg
->size
= htons(msg
->size
);
676 if (ip_vs_send_async(sock
, (char *)msg
, msize
) != msize
)
677 IP_VS_ERR("ip_vs_send_async error\n");
681 ip_vs_receive(struct socket
*sock
, char *buffer
, const size_t buflen
)
683 struct msghdr msg
= {NULL
,};
689 /* Receive a packet */
690 iov
.iov_base
= buffer
;
691 iov
.iov_len
= (size_t)buflen
;
693 len
= kernel_recvmsg(sock
, &msg
, &iov
, 1, buflen
, 0);
703 static DECLARE_WAIT_QUEUE_HEAD(sync_wait
);
704 static pid_t sync_master_pid
= 0;
705 static pid_t sync_backup_pid
= 0;
707 static DECLARE_WAIT_QUEUE_HEAD(stop_sync_wait
);
708 static int stop_master_sync
= 0;
709 static int stop_backup_sync
= 0;
711 static void sync_master_loop(void)
714 struct ip_vs_sync_buff
*sb
;
716 /* create the sending multicast socket */
717 sock
= make_send_sock();
721 IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, "
723 ip_vs_master_mcast_ifn
, ip_vs_master_syncid
);
726 while ((sb
=sb_dequeue())) {
727 ip_vs_send_sync_msg(sock
, sb
->mesg
);
728 ip_vs_sync_buff_release(sb
);
731 /* check if entries stay in curr_sb for 2 seconds */
732 if ((sb
= get_curr_sync_buff(2*HZ
))) {
733 ip_vs_send_sync_msg(sock
, sb
->mesg
);
734 ip_vs_sync_buff_release(sb
);
737 if (stop_master_sync
)
740 msleep_interruptible(1000);
743 /* clean up the sync_buff queue */
744 while ((sb
=sb_dequeue())) {
745 ip_vs_sync_buff_release(sb
);
748 /* clean up the current sync_buff */
749 if ((sb
= get_curr_sync_buff(0))) {
750 ip_vs_sync_buff_release(sb
);
753 /* release the sending multicast socket */
758 static void sync_backup_loop(void)
764 if (!(buf
= kmalloc(sync_recv_mesg_maxlen
, GFP_ATOMIC
))) {
765 IP_VS_ERR("sync_backup_loop: kmalloc error\n");
769 /* create the receiving multicast socket */
770 sock
= make_receive_sock();
774 IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
776 ip_vs_backup_mcast_ifn
, ip_vs_backup_syncid
);
779 /* do you have data now? */
780 while (!skb_queue_empty(&(sock
->sk
->sk_receive_queue
))) {
782 ip_vs_receive(sock
, buf
,
783 sync_recv_mesg_maxlen
)) <= 0) {
784 IP_VS_ERR("receiving message error\n");
787 /* disable bottom half, because it accessed the data
788 shared by softirq while getting/creating conns */
790 ip_vs_process_message(buf
, len
);
794 if (stop_backup_sync
)
797 msleep_interruptible(1000);
800 /* release the sending multicast socket */
808 static void set_sync_pid(int sync_state
, pid_t sync_pid
)
810 if (sync_state
== IP_VS_STATE_MASTER
)
811 sync_master_pid
= sync_pid
;
812 else if (sync_state
== IP_VS_STATE_BACKUP
)
813 sync_backup_pid
= sync_pid
;
816 static void set_stop_sync(int sync_state
, int set
)
818 if (sync_state
== IP_VS_STATE_MASTER
)
819 stop_master_sync
= set
;
820 else if (sync_state
== IP_VS_STATE_BACKUP
)
821 stop_backup_sync
= set
;
823 stop_master_sync
= set
;
824 stop_backup_sync
= set
;
828 static int sync_thread(void *startup
)
830 DECLARE_WAITQUEUE(wait
, current
);
834 struct ip_vs_sync_thread_data
*tinfo
= startup
;
836 /* increase the module use count */
837 ip_vs_use_count_inc();
839 if (ip_vs_sync_state
& IP_VS_STATE_MASTER
&& !sync_master_pid
) {
840 state
= IP_VS_STATE_MASTER
;
841 name
= "ipvs_syncmaster";
842 } else if (ip_vs_sync_state
& IP_VS_STATE_BACKUP
&& !sync_backup_pid
) {
843 state
= IP_VS_STATE_BACKUP
;
844 name
= "ipvs_syncbackup";
847 ip_vs_use_count_dec();
856 /* Block all signals */
857 spin_lock_irq(¤t
->sighand
->siglock
);
858 siginitsetinv(¤t
->blocked
, 0);
860 spin_unlock_irq(¤t
->sighand
->siglock
);
862 /* set the maximum length of sync message */
863 set_sync_mesg_maxlen(state
);
865 /* set up multicast address */
866 mcast_addr
.sin_family
= AF_INET
;
867 mcast_addr
.sin_port
= htons(IP_VS_SYNC_PORT
);
868 mcast_addr
.sin_addr
.s_addr
= htonl(IP_VS_SYNC_GROUP
);
870 add_wait_queue(&sync_wait
, &wait
);
872 set_sync_pid(state
, task_pid_nr(current
));
873 complete(tinfo
->startup
);
876 * once we call the completion queue above, we should
877 * null out that reference, since its allocated on the
878 * stack of the creating kernel thread
880 tinfo
->startup
= NULL
;
882 /* processing master/backup loop here */
883 if (state
== IP_VS_STATE_MASTER
)
885 else if (state
== IP_VS_STATE_BACKUP
)
889 remove_wait_queue(&sync_wait
, &wait
);
894 * If we weren't explicitly stopped, then we
895 * exited in error, and should undo our state
897 if ((!stop_master_sync
) && (!stop_backup_sync
))
898 ip_vs_sync_state
-= tinfo
->state
;
900 set_sync_pid(state
, 0);
901 IP_VS_INFO("sync thread stopped!\n");
905 /* decrease the module use count */
906 ip_vs_use_count_dec();
908 set_stop_sync(state
, 0);
909 wake_up(&stop_sync_wait
);
912 * we need to free the structure that was allocated
913 * for us in start_sync_thread
920 static int fork_sync_thread(void *startup
)
924 /* fork the sync thread here, then the parent process of the
925 sync thread is the init process after this thread exits. */
927 if ((pid
= kernel_thread(sync_thread
, startup
, 0)) < 0) {
928 IP_VS_ERR("could not create sync_thread due to %d... "
930 msleep_interruptible(1000);
938 int start_sync_thread(int state
, char *mcast_ifn
, __u8 syncid
)
940 DECLARE_COMPLETION_ONSTACK(startup
);
942 struct ip_vs_sync_thread_data
*tinfo
;
944 if ((state
== IP_VS_STATE_MASTER
&& sync_master_pid
) ||
945 (state
== IP_VS_STATE_BACKUP
&& sync_backup_pid
))
949 * Note that tinfo will be freed in sync_thread on exit
951 tinfo
= kmalloc(sizeof(struct ip_vs_sync_thread_data
), GFP_KERNEL
);
955 IP_VS_DBG(7, "%s: pid %d\n", __func__
, task_pid_nr(current
));
956 IP_VS_DBG(7, "Each ip_vs_sync_conn entry need %Zd bytes\n",
957 sizeof(struct ip_vs_sync_conn
));
959 ip_vs_sync_state
|= state
;
960 if (state
== IP_VS_STATE_MASTER
) {
961 strlcpy(ip_vs_master_mcast_ifn
, mcast_ifn
,
962 sizeof(ip_vs_master_mcast_ifn
));
963 ip_vs_master_syncid
= syncid
;
965 strlcpy(ip_vs_backup_mcast_ifn
, mcast_ifn
,
966 sizeof(ip_vs_backup_mcast_ifn
));
967 ip_vs_backup_syncid
= syncid
;
970 tinfo
->state
= state
;
971 tinfo
->startup
= &startup
;
974 if ((pid
= kernel_thread(fork_sync_thread
, tinfo
, 0)) < 0) {
975 IP_VS_ERR("could not create fork_sync_thread due to %d... "
977 msleep_interruptible(1000);
981 wait_for_completion(&startup
);
987 int stop_sync_thread(int state
)
989 DECLARE_WAITQUEUE(wait
, current
);
991 if ((state
== IP_VS_STATE_MASTER
&& !sync_master_pid
) ||
992 (state
== IP_VS_STATE_BACKUP
&& !sync_backup_pid
))
995 IP_VS_DBG(7, "%s: pid %d\n", __func__
, task_pid_nr(current
));
996 IP_VS_INFO("stopping sync thread %d ...\n",
997 (state
== IP_VS_STATE_MASTER
) ?
998 sync_master_pid
: sync_backup_pid
);
1000 __set_current_state(TASK_UNINTERRUPTIBLE
);
1001 add_wait_queue(&stop_sync_wait
, &wait
);
1002 set_stop_sync(state
, 1);
1003 ip_vs_sync_state
-= state
;
1004 wake_up(&sync_wait
);
1006 __set_current_state(TASK_RUNNING
);
1007 remove_wait_queue(&stop_sync_wait
, &wait
);
1009 /* Note: no need to reap the sync thread, because its parent
1010 process is the init process */
1012 if ((state
== IP_VS_STATE_MASTER
&& stop_master_sync
) ||
1013 (state
== IP_VS_STATE_BACKUP
&& stop_backup_sync
))