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>
31 #include <linux/kthread.h>
32 #include <linux/wait.h>
33 #include <linux/kernel.h>
38 #include <net/ip_vs.h>
40 #define IP_VS_SYNC_GROUP 0xe0000051 /* multicast addr - 224.0.0.81 */
41 #define IP_VS_SYNC_PORT 8848 /* multicast port */
45 * IPVS sync connection entry
47 struct ip_vs_sync_conn
{
50 /* Protocol, addresses and port numbers */
51 __u8 protocol
; /* Which protocol (TCP/UDP) */
55 __be32 caddr
; /* client address */
56 __be32 vaddr
; /* virtual address */
57 __be32 daddr
; /* destination address */
59 /* Flags and state transition */
60 __be16 flags
; /* status flags */
61 __be16 state
; /* state info */
63 /* The sequence options start here */
66 struct ip_vs_sync_conn_options
{
67 struct ip_vs_seq in_seq
; /* incoming seq. struct */
68 struct ip_vs_seq out_seq
; /* outgoing seq. struct */
71 struct ip_vs_sync_thread_data
{
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
103 #define MAX_CONNS_PER_SYNCBUFF 255 /* nr_conns in ip_vs_sync_mesg is 8 bit */
105 struct ip_vs_sync_mesg
{
110 /* ip_vs_sync_conn entries start here */
113 /* the maximum length of sync (sending/receiving) message */
114 static int sync_send_mesg_maxlen
;
115 static int sync_recv_mesg_maxlen
;
117 struct ip_vs_sync_buff
{
118 struct list_head list
;
119 unsigned long firstuse
;
121 /* pointers for the message data */
122 struct ip_vs_sync_mesg
*mesg
;
128 /* the sync_buff list head and the lock */
129 static LIST_HEAD(ip_vs_sync_queue
);
130 static DEFINE_SPINLOCK(ip_vs_sync_lock
);
132 /* current sync_buff for accepting new conn entries */
133 static struct ip_vs_sync_buff
*curr_sb
= NULL
;
134 static DEFINE_SPINLOCK(curr_sb_lock
);
136 /* ipvs sync daemon state */
137 volatile int ip_vs_sync_state
= IP_VS_STATE_NONE
;
138 volatile int ip_vs_master_syncid
= 0;
139 volatile int ip_vs_backup_syncid
= 0;
141 /* multicast interface name */
142 char ip_vs_master_mcast_ifn
[IP_VS_IFNAME_MAXLEN
];
143 char ip_vs_backup_mcast_ifn
[IP_VS_IFNAME_MAXLEN
];
145 /* sync daemon tasks */
146 static struct task_struct
*sync_master_thread
;
147 static struct task_struct
*sync_backup_thread
;
150 static struct sockaddr_in mcast_addr
= {
151 .sin_family
= AF_INET
,
152 .sin_port
= __constant_htons(IP_VS_SYNC_PORT
),
153 .sin_addr
.s_addr
= __constant_htonl(IP_VS_SYNC_GROUP
),
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
)
201 static inline void sb_queue_tail(struct ip_vs_sync_buff
*sb
)
203 spin_lock(&ip_vs_sync_lock
);
204 if (ip_vs_sync_state
& IP_VS_STATE_MASTER
)
205 list_add_tail(&sb
->list
, &ip_vs_sync_queue
);
207 ip_vs_sync_buff_release(sb
);
208 spin_unlock(&ip_vs_sync_lock
);
212 * Get the current sync buffer if it has been created for more
213 * than the specified time or the specified time is zero.
215 static inline struct ip_vs_sync_buff
*
216 get_curr_sync_buff(unsigned long time
)
218 struct ip_vs_sync_buff
*sb
;
220 spin_lock_bh(&curr_sb_lock
);
221 if (curr_sb
&& (time
== 0 ||
222 time_before(jiffies
- curr_sb
->firstuse
, time
))) {
227 spin_unlock_bh(&curr_sb_lock
);
233 * Add an ip_vs_conn information into the current sync_buff.
234 * Called by ip_vs_in.
236 void ip_vs_sync_conn(struct ip_vs_conn
*cp
)
238 struct ip_vs_sync_mesg
*m
;
239 struct ip_vs_sync_conn
*s
;
242 spin_lock(&curr_sb_lock
);
244 if (!(curr_sb
=ip_vs_sync_buff_create())) {
245 spin_unlock(&curr_sb_lock
);
246 IP_VS_ERR("ip_vs_sync_buff_create failed.\n");
251 len
= (cp
->flags
& IP_VS_CONN_F_SEQ_MASK
) ? FULL_CONN_SIZE
:
254 s
= (struct ip_vs_sync_conn
*)curr_sb
->head
;
257 s
->protocol
= cp
->protocol
;
258 s
->cport
= cp
->cport
;
259 s
->vport
= cp
->vport
;
260 s
->dport
= cp
->dport
;
261 s
->caddr
= cp
->caddr
.ip
;
262 s
->vaddr
= cp
->vaddr
.ip
;
263 s
->daddr
= cp
->daddr
.ip
;
264 s
->flags
= htons(cp
->flags
& ~IP_VS_CONN_F_HASHED
);
265 s
->state
= htons(cp
->state
);
266 if (cp
->flags
& IP_VS_CONN_F_SEQ_MASK
) {
267 struct ip_vs_sync_conn_options
*opt
=
268 (struct ip_vs_sync_conn_options
*)&s
[1];
269 memcpy(opt
, &cp
->in_seq
, sizeof(*opt
));
274 curr_sb
->head
+= len
;
276 /* check if there is a space for next one */
277 if (curr_sb
->head
+FULL_CONN_SIZE
> curr_sb
->end
) {
278 sb_queue_tail(curr_sb
);
281 spin_unlock(&curr_sb_lock
);
283 /* synchronize its controller if it has */
285 ip_vs_sync_conn(cp
->control
);
290 * Process received multicast message and create the corresponding
291 * ip_vs_conn entries.
293 static void ip_vs_process_message(const char *buffer
, const size_t buflen
)
295 struct ip_vs_sync_mesg
*m
= (struct ip_vs_sync_mesg
*)buffer
;
296 struct ip_vs_sync_conn
*s
;
297 struct ip_vs_sync_conn_options
*opt
;
298 struct ip_vs_conn
*cp
;
299 struct ip_vs_protocol
*pp
;
300 struct ip_vs_dest
*dest
;
304 if (buflen
< sizeof(struct ip_vs_sync_mesg
)) {
305 IP_VS_ERR_RL("sync message header too short\n");
309 /* Convert size back to host byte order */
310 m
->size
= ntohs(m
->size
);
312 if (buflen
!= m
->size
) {
313 IP_VS_ERR_RL("bogus sync message size\n");
317 /* SyncID sanity check */
318 if (ip_vs_backup_syncid
!= 0 && m
->syncid
!= ip_vs_backup_syncid
) {
319 IP_VS_DBG(7, "Ignoring incoming msg with syncid = %d\n",
324 p
= (char *)buffer
+ sizeof(struct ip_vs_sync_mesg
);
325 for (i
=0; i
<m
->nr_conns
; i
++) {
326 unsigned flags
, state
;
328 if (p
+ SIMPLE_CONN_SIZE
> buffer
+buflen
) {
329 IP_VS_ERR_RL("bogus conn in sync message\n");
332 s
= (struct ip_vs_sync_conn
*) p
;
333 flags
= ntohs(s
->flags
) | IP_VS_CONN_F_SYNC
;
334 flags
&= ~IP_VS_CONN_F_HASHED
;
335 if (flags
& IP_VS_CONN_F_SEQ_MASK
) {
336 opt
= (struct ip_vs_sync_conn_options
*)&s
[1];
338 if (p
> buffer
+buflen
) {
339 IP_VS_ERR_RL("bogus conn options in sync message\n");
344 p
+= SIMPLE_CONN_SIZE
;
347 state
= ntohs(s
->state
);
348 if (!(flags
& IP_VS_CONN_F_TEMPLATE
)) {
349 pp
= ip_vs_proto_get(s
->protocol
);
351 IP_VS_ERR_RL("Unsupported protocol %u in sync msg\n",
355 if (state
>= pp
->num_states
) {
356 IP_VS_DBG(2, "Invalid %s state %u in sync msg\n",
361 /* protocol in templates is not used for state/timeout */
364 IP_VS_DBG(2, "Invalid template state %u in sync msg\n",
370 if (!(flags
& IP_VS_CONN_F_TEMPLATE
))
371 cp
= ip_vs_conn_in_get(AF_INET
, s
->protocol
,
372 (union nf_inet_addr
*)&s
->caddr
,
374 (union nf_inet_addr
*)&s
->vaddr
,
377 cp
= ip_vs_ct_in_get(AF_INET
, s
->protocol
,
378 (union nf_inet_addr
*)&s
->caddr
,
380 (union nf_inet_addr
*)&s
->vaddr
,
384 * Find the appropriate destination for the connection.
385 * If it is not found the connection will remain unbound
388 dest
= ip_vs_find_dest(AF_INET
,
389 (union nf_inet_addr
*)&s
->daddr
,
391 (union nf_inet_addr
*)&s
->vaddr
,
394 /* Set the approprite ativity flag */
395 if (s
->protocol
== IPPROTO_TCP
) {
396 if (state
!= IP_VS_TCP_S_ESTABLISHED
)
397 flags
|= IP_VS_CONN_F_INACTIVE
;
399 flags
&= ~IP_VS_CONN_F_INACTIVE
;
401 cp
= ip_vs_conn_new(AF_INET
, s
->protocol
,
402 (union nf_inet_addr
*)&s
->caddr
,
404 (union nf_inet_addr
*)&s
->vaddr
,
406 (union nf_inet_addr
*)&s
->daddr
,
410 atomic_dec(&dest
->refcnt
);
412 IP_VS_ERR("ip_vs_conn_new failed\n");
415 } else if (!cp
->dest
) {
416 dest
= ip_vs_try_bind_dest(cp
);
418 atomic_dec(&dest
->refcnt
);
419 } else if ((cp
->dest
) && (cp
->protocol
== IPPROTO_TCP
) &&
420 (cp
->state
!= state
)) {
421 /* update active/inactive flag for the connection */
423 if (!(cp
->flags
& IP_VS_CONN_F_INACTIVE
) &&
424 (state
!= IP_VS_TCP_S_ESTABLISHED
)) {
425 atomic_dec(&dest
->activeconns
);
426 atomic_inc(&dest
->inactconns
);
427 cp
->flags
|= IP_VS_CONN_F_INACTIVE
;
428 } else if ((cp
->flags
& IP_VS_CONN_F_INACTIVE
) &&
429 (state
== IP_VS_TCP_S_ESTABLISHED
)) {
430 atomic_inc(&dest
->activeconns
);
431 atomic_dec(&dest
->inactconns
);
432 cp
->flags
&= ~IP_VS_CONN_F_INACTIVE
;
437 memcpy(&cp
->in_seq
, opt
, sizeof(*opt
));
438 atomic_set(&cp
->in_pkts
, sysctl_ip_vs_sync_threshold
[0]);
440 cp
->old_state
= cp
->state
;
442 * We can not recover the right timeout for templates
443 * in all cases, we can not find the right fwmark
444 * virtual service. If needed, we can do it for
445 * non-fwmark persistent services.
447 if (!(flags
& IP_VS_CONN_F_TEMPLATE
) && pp
->timeout_table
)
448 cp
->timeout
= pp
->timeout_table
[state
];
450 cp
->timeout
= (3*60*HZ
);
457 * Setup loopback of outgoing multicasts on a sending socket
459 static void set_mcast_loop(struct sock
*sk
, u_char loop
)
461 struct inet_sock
*inet
= inet_sk(sk
);
463 /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
465 inet
->mc_loop
= loop
? 1 : 0;
470 * Specify TTL for outgoing multicasts on a sending socket
472 static void set_mcast_ttl(struct sock
*sk
, u_char ttl
)
474 struct inet_sock
*inet
= inet_sk(sk
);
476 /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
483 * Specifiy default interface for outgoing multicasts
485 static int set_mcast_if(struct sock
*sk
, char *ifname
)
487 struct net_device
*dev
;
488 struct inet_sock
*inet
= inet_sk(sk
);
490 if ((dev
= __dev_get_by_name(&init_net
, ifname
)) == NULL
)
493 if (sk
->sk_bound_dev_if
&& dev
->ifindex
!= sk
->sk_bound_dev_if
)
497 inet
->mc_index
= dev
->ifindex
;
498 /* inet->mc_addr = 0; */
506 * Set the maximum length of sync message according to the
507 * specified interface's MTU.
509 static int set_sync_mesg_maxlen(int sync_state
)
511 struct net_device
*dev
;
514 if (sync_state
== IP_VS_STATE_MASTER
) {
515 if ((dev
= __dev_get_by_name(&init_net
, ip_vs_master_mcast_ifn
)) == NULL
)
518 num
= (dev
->mtu
- sizeof(struct iphdr
) -
519 sizeof(struct udphdr
) -
520 SYNC_MESG_HEADER_LEN
- 20) / SIMPLE_CONN_SIZE
;
521 sync_send_mesg_maxlen
= SYNC_MESG_HEADER_LEN
+
522 SIMPLE_CONN_SIZE
* min(num
, MAX_CONNS_PER_SYNCBUFF
);
523 IP_VS_DBG(7, "setting the maximum length of sync sending "
524 "message %d.\n", sync_send_mesg_maxlen
);
525 } else if (sync_state
== IP_VS_STATE_BACKUP
) {
526 if ((dev
= __dev_get_by_name(&init_net
, ip_vs_backup_mcast_ifn
)) == NULL
)
529 sync_recv_mesg_maxlen
= dev
->mtu
-
530 sizeof(struct iphdr
) - sizeof(struct udphdr
);
531 IP_VS_DBG(7, "setting the maximum length of sync receiving "
532 "message %d.\n", sync_recv_mesg_maxlen
);
540 * Join a multicast group.
541 * the group is specified by a class D multicast address 224.0.0.0/8
542 * in the in_addr structure passed in as a parameter.
545 join_mcast_group(struct sock
*sk
, struct in_addr
*addr
, char *ifname
)
547 struct ip_mreqn mreq
;
548 struct net_device
*dev
;
551 memset(&mreq
, 0, sizeof(mreq
));
552 memcpy(&mreq
.imr_multiaddr
, addr
, sizeof(struct in_addr
));
554 if ((dev
= __dev_get_by_name(&init_net
, ifname
)) == NULL
)
556 if (sk
->sk_bound_dev_if
&& dev
->ifindex
!= sk
->sk_bound_dev_if
)
559 mreq
.imr_ifindex
= dev
->ifindex
;
562 ret
= ip_mc_join_group(sk
, &mreq
);
569 static int bind_mcastif_addr(struct socket
*sock
, char *ifname
)
571 struct net_device
*dev
;
573 struct sockaddr_in sin
;
575 if ((dev
= __dev_get_by_name(&init_net
, ifname
)) == NULL
)
578 addr
= inet_select_addr(dev
, 0, RT_SCOPE_UNIVERSE
);
580 IP_VS_ERR("You probably need to specify IP address on "
581 "multicast interface.\n");
583 IP_VS_DBG(7, "binding socket with (%s) %u.%u.%u.%u\n",
584 ifname
, NIPQUAD(addr
));
586 /* Now bind the socket with the address of multicast interface */
587 sin
.sin_family
= AF_INET
;
588 sin
.sin_addr
.s_addr
= addr
;
591 return sock
->ops
->bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
));
595 * Set up sending multicast socket over UDP
597 static struct socket
* make_send_sock(void)
602 /* First create a socket */
603 result
= sock_create_kern(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
);
605 IP_VS_ERR("Error during creation of socket; terminating\n");
606 return ERR_PTR(result
);
609 result
= set_mcast_if(sock
->sk
, ip_vs_master_mcast_ifn
);
611 IP_VS_ERR("Error setting outbound mcast interface\n");
615 set_mcast_loop(sock
->sk
, 0);
616 set_mcast_ttl(sock
->sk
, 1);
618 result
= bind_mcastif_addr(sock
, ip_vs_master_mcast_ifn
);
620 IP_VS_ERR("Error binding address of the mcast interface\n");
624 result
= sock
->ops
->connect(sock
, (struct sockaddr
*) &mcast_addr
,
625 sizeof(struct sockaddr
), 0);
627 IP_VS_ERR("Error connecting to the multicast addr\n");
635 return ERR_PTR(result
);
640 * Set up receiving multicast socket over UDP
642 static struct socket
* make_receive_sock(void)
647 /* First create a socket */
648 result
= sock_create_kern(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
);
650 IP_VS_ERR("Error during creation of socket; terminating\n");
651 return ERR_PTR(result
);
654 /* it is equivalent to the REUSEADDR option in user-space */
655 sock
->sk
->sk_reuse
= 1;
657 result
= sock
->ops
->bind(sock
, (struct sockaddr
*) &mcast_addr
,
658 sizeof(struct sockaddr
));
660 IP_VS_ERR("Error binding to the multicast addr\n");
664 /* join the multicast group */
665 result
= join_mcast_group(sock
->sk
,
666 (struct in_addr
*) &mcast_addr
.sin_addr
,
667 ip_vs_backup_mcast_ifn
);
669 IP_VS_ERR("Error joining to the multicast group\n");
677 return ERR_PTR(result
);
682 ip_vs_send_async(struct socket
*sock
, const char *buffer
, const size_t length
)
684 struct msghdr msg
= {.msg_flags
= MSG_DONTWAIT
|MSG_NOSIGNAL
};
689 iov
.iov_base
= (void *)buffer
;
690 iov
.iov_len
= length
;
692 len
= kernel_sendmsg(sock
, &msg
, &iov
, 1, (size_t)(length
));
699 ip_vs_send_sync_msg(struct socket
*sock
, struct ip_vs_sync_mesg
*msg
)
705 /* Put size in network byte order */
706 msg
->size
= htons(msg
->size
);
708 if (ip_vs_send_async(sock
, (char *)msg
, msize
) != msize
)
709 IP_VS_ERR("ip_vs_send_async error\n");
713 ip_vs_receive(struct socket
*sock
, char *buffer
, const size_t buflen
)
715 struct msghdr msg
= {NULL
,};
721 /* Receive a packet */
722 iov
.iov_base
= buffer
;
723 iov
.iov_len
= (size_t)buflen
;
725 len
= kernel_recvmsg(sock
, &msg
, &iov
, 1, buflen
, 0);
735 static int sync_thread_master(void *data
)
737 struct ip_vs_sync_thread_data
*tinfo
= data
;
738 struct ip_vs_sync_buff
*sb
;
740 IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, "
742 ip_vs_master_mcast_ifn
, ip_vs_master_syncid
);
744 while (!kthread_should_stop()) {
745 while ((sb
= sb_dequeue())) {
746 ip_vs_send_sync_msg(tinfo
->sock
, sb
->mesg
);
747 ip_vs_sync_buff_release(sb
);
750 /* check if entries stay in curr_sb for 2 seconds */
751 sb
= get_curr_sync_buff(2 * HZ
);
753 ip_vs_send_sync_msg(tinfo
->sock
, sb
->mesg
);
754 ip_vs_sync_buff_release(sb
);
757 schedule_timeout_interruptible(HZ
);
760 /* clean up the sync_buff queue */
761 while ((sb
=sb_dequeue())) {
762 ip_vs_sync_buff_release(sb
);
765 /* clean up the current sync_buff */
766 if ((sb
= get_curr_sync_buff(0))) {
767 ip_vs_sync_buff_release(sb
);
770 /* release the sending multicast socket */
771 sock_release(tinfo
->sock
);
778 static int sync_thread_backup(void *data
)
780 struct ip_vs_sync_thread_data
*tinfo
= data
;
783 IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
785 ip_vs_backup_mcast_ifn
, ip_vs_backup_syncid
);
787 while (!kthread_should_stop()) {
788 wait_event_interruptible(*tinfo
->sock
->sk
->sk_sleep
,
789 !skb_queue_empty(&tinfo
->sock
->sk
->sk_receive_queue
)
790 || kthread_should_stop());
792 /* do we have data now? */
793 while (!skb_queue_empty(&(tinfo
->sock
->sk
->sk_receive_queue
))) {
794 len
= ip_vs_receive(tinfo
->sock
, tinfo
->buf
,
795 sync_recv_mesg_maxlen
);
797 IP_VS_ERR("receiving message error\n");
801 /* disable bottom half, because it accesses the data
802 shared by softirq while getting/creating conns */
804 ip_vs_process_message(tinfo
->buf
, len
);
809 /* release the sending multicast socket */
810 sock_release(tinfo
->sock
);
818 int start_sync_thread(int state
, char *mcast_ifn
, __u8 syncid
)
820 struct ip_vs_sync_thread_data
*tinfo
;
821 struct task_struct
**realtask
, *task
;
823 char *name
, *buf
= NULL
;
824 int (*threadfn
)(void *data
);
825 int result
= -ENOMEM
;
827 IP_VS_DBG(7, "%s: pid %d\n", __func__
, task_pid_nr(current
));
828 IP_VS_DBG(7, "Each ip_vs_sync_conn entry needs %Zd bytes\n",
829 sizeof(struct ip_vs_sync_conn
));
831 if (state
== IP_VS_STATE_MASTER
) {
832 if (sync_master_thread
)
835 strlcpy(ip_vs_master_mcast_ifn
, mcast_ifn
,
836 sizeof(ip_vs_master_mcast_ifn
));
837 ip_vs_master_syncid
= syncid
;
838 realtask
= &sync_master_thread
;
839 name
= "ipvs_syncmaster";
840 threadfn
= sync_thread_master
;
841 sock
= make_send_sock();
842 } else if (state
== IP_VS_STATE_BACKUP
) {
843 if (sync_backup_thread
)
846 strlcpy(ip_vs_backup_mcast_ifn
, mcast_ifn
,
847 sizeof(ip_vs_backup_mcast_ifn
));
848 ip_vs_backup_syncid
= syncid
;
849 realtask
= &sync_backup_thread
;
850 name
= "ipvs_syncbackup";
851 threadfn
= sync_thread_backup
;
852 sock
= make_receive_sock();
858 result
= PTR_ERR(sock
);
862 set_sync_mesg_maxlen(state
);
863 if (state
== IP_VS_STATE_BACKUP
) {
864 buf
= kmalloc(sync_recv_mesg_maxlen
, GFP_KERNEL
);
869 tinfo
= kmalloc(sizeof(*tinfo
), GFP_KERNEL
);
876 task
= kthread_run(threadfn
, tinfo
, name
);
878 result
= PTR_ERR(task
);
884 ip_vs_sync_state
|= state
;
886 /* increase the module use count */
887 ip_vs_use_count_inc();
902 int stop_sync_thread(int state
)
904 IP_VS_DBG(7, "%s: pid %d\n", __func__
, task_pid_nr(current
));
906 if (state
== IP_VS_STATE_MASTER
) {
907 if (!sync_master_thread
)
910 IP_VS_INFO("stopping master sync thread %d ...\n",
911 task_pid_nr(sync_master_thread
));
914 * The lock synchronizes with sb_queue_tail(), so that we don't
915 * add sync buffers to the queue, when we are already in
916 * progress of stopping the master sync daemon.
919 spin_lock_bh(&ip_vs_sync_lock
);
920 ip_vs_sync_state
&= ~IP_VS_STATE_MASTER
;
921 spin_unlock_bh(&ip_vs_sync_lock
);
922 kthread_stop(sync_master_thread
);
923 sync_master_thread
= NULL
;
924 } else if (state
== IP_VS_STATE_BACKUP
) {
925 if (!sync_backup_thread
)
928 IP_VS_INFO("stopping backup sync thread %d ...\n",
929 task_pid_nr(sync_backup_thread
));
931 ip_vs_sync_state
&= ~IP_VS_STATE_BACKUP
;
932 kthread_stop(sync_backup_thread
);
933 sync_backup_thread
= NULL
;
938 /* decrease the module use count */
939 ip_vs_use_count_dec();