net: replace NIPQUAD() in net/netfilter/
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netfilter / ipvs / ip_vs_sync.c
blob6be5d4efa51ba58c9f13bb40b475bcb58c6795ca
1 /*
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
6 * cluster of servers.
8 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
10 * ip_vs_sync: sync connection info from master load balancer to backups
11 * through multicast
13 * Changes:
14 * Alexandre Cassen : Added master & backup support at a time.
15 * Alexandre Cassen : Added SyncID support for incoming sync
16 * messages filtering.
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>
27 #include <linux/in.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>
35 #include <net/ip.h>
36 #include <net/sock.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 {
48 __u8 reserved;
50 /* Protocol, addresses and port numbers */
51 __u8 protocol; /* Which protocol (TCP/UDP) */
52 __be16 cport;
53 __be16 vport;
54 __be16 dport;
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 {
72 struct socket *sock;
73 char *buf;
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
83 following format.
85 0 1 2 3
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90 | |
91 | IPVS Sync Connection (1) |
92 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93 | . |
94 | . |
95 | . |
96 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97 | |
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 {
106 __u8 nr_conns;
107 __u8 syncid;
108 __u16 size;
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;
123 unsigned char *head;
124 unsigned char *end;
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;
149 /* multicast addr */
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)) {
163 sb = NULL;
164 } else {
165 sb = list_entry(ip_vs_sync_queue.next,
166 struct ip_vs_sync_buff,
167 list);
168 list_del(&sb->list);
170 spin_unlock_bh(&ip_vs_sync_lock);
172 return sb;
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)))
180 return NULL;
182 if (!(sb->mesg=kmalloc(sync_send_mesg_maxlen, GFP_ATOMIC))) {
183 kfree(sb);
184 return NULL;
186 sb->mesg->nr_conns = 0;
187 sb->mesg->syncid = ip_vs_master_syncid;
188 sb->mesg->size = 4;
189 sb->head = (unsigned char *)sb->mesg + 4;
190 sb->end = (unsigned char *)sb->mesg + sync_send_mesg_maxlen;
191 sb->firstuse = jiffies;
192 return sb;
195 static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff *sb)
197 kfree(sb->mesg);
198 kfree(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);
206 else
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))) {
223 sb = curr_sb;
224 curr_sb = NULL;
225 } else
226 sb = NULL;
227 spin_unlock_bh(&curr_sb_lock);
228 return sb;
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;
240 int len;
242 spin_lock(&curr_sb_lock);
243 if (!curr_sb) {
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");
247 return;
251 len = (cp->flags & IP_VS_CONN_F_SEQ_MASK) ? FULL_CONN_SIZE :
252 SIMPLE_CONN_SIZE;
253 m = curr_sb->mesg;
254 s = (struct ip_vs_sync_conn *)curr_sb->head;
256 /* copy members */
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));
272 m->nr_conns++;
273 m->size += len;
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);
279 curr_sb = NULL;
281 spin_unlock(&curr_sb_lock);
283 /* synchronize its controller if it has */
284 if (cp->control)
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;
301 char *p;
302 int i;
304 if (buflen < sizeof(struct ip_vs_sync_mesg)) {
305 IP_VS_ERR_RL("sync message header too short\n");
306 return;
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");
314 return;
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",
320 m->syncid);
321 return;
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");
330 return;
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];
337 p += FULL_CONN_SIZE;
338 if (p > buffer+buflen) {
339 IP_VS_ERR_RL("bogus conn options in sync message\n");
340 return;
342 } else {
343 opt = NULL;
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);
350 if (!pp) {
351 IP_VS_ERR_RL("Unsupported protocol %u in sync msg\n",
352 s->protocol);
353 continue;
355 if (state >= pp->num_states) {
356 IP_VS_DBG(2, "Invalid %s state %u in sync msg\n",
357 pp->name, state);
358 continue;
360 } else {
361 /* protocol in templates is not used for state/timeout */
362 pp = NULL;
363 if (state > 0) {
364 IP_VS_DBG(2, "Invalid template state %u in sync msg\n",
365 state);
366 state = 0;
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,
373 s->cport,
374 (union nf_inet_addr *)&s->vaddr,
375 s->vport);
376 else
377 cp = ip_vs_ct_in_get(AF_INET, s->protocol,
378 (union nf_inet_addr *)&s->caddr,
379 s->cport,
380 (union nf_inet_addr *)&s->vaddr,
381 s->vport);
382 if (!cp) {
384 * Find the appropriate destination for the connection.
385 * If it is not found the connection will remain unbound
386 * but still handled.
388 dest = ip_vs_find_dest(AF_INET,
389 (union nf_inet_addr *)&s->daddr,
390 s->dport,
391 (union nf_inet_addr *)&s->vaddr,
392 s->vport,
393 s->protocol);
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;
398 else
399 flags &= ~IP_VS_CONN_F_INACTIVE;
401 cp = ip_vs_conn_new(AF_INET, s->protocol,
402 (union nf_inet_addr *)&s->caddr,
403 s->cport,
404 (union nf_inet_addr *)&s->vaddr,
405 s->vport,
406 (union nf_inet_addr *)&s->daddr,
407 s->dport,
408 flags, dest);
409 if (dest)
410 atomic_dec(&dest->refcnt);
411 if (!cp) {
412 IP_VS_ERR("ip_vs_conn_new failed\n");
413 return;
415 } else if (!cp->dest) {
416 dest = ip_vs_try_bind_dest(cp);
417 if (dest)
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 */
422 dest = cp->dest;
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;
436 if (opt)
437 memcpy(&cp->in_seq, opt, sizeof(*opt));
438 atomic_set(&cp->in_pkts, sysctl_ip_vs_sync_threshold[0]);
439 cp->state = state;
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];
449 else
450 cp->timeout = (3*60*HZ);
451 ip_vs_conn_put(cp);
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)); */
464 lock_sock(sk);
465 inet->mc_loop = loop ? 1 : 0;
466 release_sock(sk);
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)); */
477 lock_sock(sk);
478 inet->mc_ttl = ttl;
479 release_sock(sk);
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)
491 return -ENODEV;
493 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
494 return -EINVAL;
496 lock_sock(sk);
497 inet->mc_index = dev->ifindex;
498 /* inet->mc_addr = 0; */
499 release_sock(sk);
501 return 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;
512 int num;
514 if (sync_state == IP_VS_STATE_MASTER) {
515 if ((dev = __dev_get_by_name(&init_net, ip_vs_master_mcast_ifn)) == NULL)
516 return -ENODEV;
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)
527 return -ENODEV;
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);
535 return 0;
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.
544 static int
545 join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
547 struct ip_mreqn mreq;
548 struct net_device *dev;
549 int ret;
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)
555 return -ENODEV;
556 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
557 return -EINVAL;
559 mreq.imr_ifindex = dev->ifindex;
561 lock_sock(sk);
562 ret = ip_mc_join_group(sk, &mreq);
563 release_sock(sk);
565 return ret;
569 static int bind_mcastif_addr(struct socket *sock, char *ifname)
571 struct net_device *dev;
572 __be32 addr;
573 struct sockaddr_in sin;
575 if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
576 return -ENODEV;
578 addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
579 if (!addr)
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) %pI4\n",
584 ifname, &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;
589 sin.sin_port = 0;
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)
599 struct socket *sock;
600 int result;
602 /* First create a socket */
603 result = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
604 if (result < 0) {
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);
610 if (result < 0) {
611 IP_VS_ERR("Error setting outbound mcast interface\n");
612 goto error;
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);
619 if (result < 0) {
620 IP_VS_ERR("Error binding address of the mcast interface\n");
621 goto error;
624 result = sock->ops->connect(sock, (struct sockaddr *) &mcast_addr,
625 sizeof(struct sockaddr), 0);
626 if (result < 0) {
627 IP_VS_ERR("Error connecting to the multicast addr\n");
628 goto error;
631 return sock;
633 error:
634 sock_release(sock);
635 return ERR_PTR(result);
640 * Set up receiving multicast socket over UDP
642 static struct socket * make_receive_sock(void)
644 struct socket *sock;
645 int result;
647 /* First create a socket */
648 result = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
649 if (result < 0) {
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));
659 if (result < 0) {
660 IP_VS_ERR("Error binding to the multicast addr\n");
661 goto error;
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);
668 if (result < 0) {
669 IP_VS_ERR("Error joining to the multicast group\n");
670 goto error;
673 return sock;
675 error:
676 sock_release(sock);
677 return ERR_PTR(result);
681 static int
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};
685 struct kvec iov;
686 int len;
688 EnterFunction(7);
689 iov.iov_base = (void *)buffer;
690 iov.iov_len = length;
692 len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
694 LeaveFunction(7);
695 return len;
698 static void
699 ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
701 int msize;
703 msize = msg->size;
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");
712 static int
713 ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
715 struct msghdr msg = {NULL,};
716 struct kvec iov;
717 int len;
719 EnterFunction(7);
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);
727 if (len < 0)
728 return -1;
730 LeaveFunction(7);
731 return len;
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, "
741 "syncid = %d\n",
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);
752 if (sb) {
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);
772 kfree(tinfo);
774 return 0;
778 static int sync_thread_backup(void *data)
780 struct ip_vs_sync_thread_data *tinfo = data;
781 int len;
783 IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
784 "syncid = %d\n",
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);
796 if (len <= 0) {
797 IP_VS_ERR("receiving message error\n");
798 break;
801 /* disable bottom half, because it accesses the data
802 shared by softirq while getting/creating conns */
803 local_bh_disable();
804 ip_vs_process_message(tinfo->buf, len);
805 local_bh_enable();
809 /* release the sending multicast socket */
810 sock_release(tinfo->sock);
811 kfree(tinfo->buf);
812 kfree(tinfo);
814 return 0;
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;
822 struct socket *sock;
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)
833 return -EEXIST;
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)
844 return -EEXIST;
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();
853 } else {
854 return -EINVAL;
857 if (IS_ERR(sock)) {
858 result = PTR_ERR(sock);
859 goto out;
862 set_sync_mesg_maxlen(state);
863 if (state == IP_VS_STATE_BACKUP) {
864 buf = kmalloc(sync_recv_mesg_maxlen, GFP_KERNEL);
865 if (!buf)
866 goto outsocket;
869 tinfo = kmalloc(sizeof(*tinfo), GFP_KERNEL);
870 if (!tinfo)
871 goto outbuf;
873 tinfo->sock = sock;
874 tinfo->buf = buf;
876 task = kthread_run(threadfn, tinfo, name);
877 if (IS_ERR(task)) {
878 result = PTR_ERR(task);
879 goto outtinfo;
882 /* mark as active */
883 *realtask = task;
884 ip_vs_sync_state |= state;
886 /* increase the module use count */
887 ip_vs_use_count_inc();
889 return 0;
891 outtinfo:
892 kfree(tinfo);
893 outbuf:
894 kfree(buf);
895 outsocket:
896 sock_release(sock);
897 out:
898 return result;
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)
908 return -ESRCH;
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)
926 return -ESRCH;
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;
934 } else {
935 return -EINVAL;
938 /* decrease the module use count */
939 ip_vs_use_count_dec();
941 return 0;