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/net.h>
25 #include <linux/completion.h>
26 #include <linux/delay.h>
27 #include <linux/skbuff.h>
29 #include <linux/igmp.h> /* for ip_mc_join_group */
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 __u32 caddr; /* client address */
53 __u32 vaddr; /* virtual address */
54 __u32 daddr; /* destination address */
56 /* Flags and state transition */
57 __u16 flags; /* status flags */
58 __u16 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 #define IP_VS_SYNC_CONN_TIMEOUT (3*60*HZ)
69 #define SIMPLE_CONN_SIZE (sizeof(struct ip_vs_sync_conn))
70 #define FULL_CONN_SIZE \
71 (sizeof(struct ip_vs_sync_conn) + sizeof(struct ip_vs_sync_conn_options))
75 The master mulitcasts messages to the backup load balancers in the
79 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
80 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
81 | Count Conns | SyncID | Size |
82 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
84 | IPVS Sync Connection (1) |
85 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
89 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91 | IPVS Sync Connection (n) |
92 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95 #define SYNC_MESG_HEADER_LEN 4
97 struct ip_vs_sync_mesg {
102 /* ip_vs_sync_conn entries start here */
105 /* the maximum length of sync (sending/receiving) message */
106 static int sync_send_mesg_maxlen;
107 static int sync_recv_mesg_maxlen;
109 struct ip_vs_sync_buff {
110 struct list_head list;
111 unsigned long firstuse;
113 /* pointers for the message data */
114 struct ip_vs_sync_mesg *mesg;
120 /* the sync_buff list head and the lock */
121 static LIST_HEAD(ip_vs_sync_queue);
122 static DEFINE_SPINLOCK(ip_vs_sync_lock);
124 /* current sync_buff for accepting new conn entries */
125 static struct ip_vs_sync_buff *curr_sb = NULL;
126 static DEFINE_SPINLOCK(curr_sb_lock);
128 /* ipvs sync daemon state */
129 volatile int ip_vs_sync_state = IP_VS_STATE_NONE;
130 volatile int ip_vs_master_syncid = 0;
131 volatile int ip_vs_backup_syncid = 0;
133 /* multicast interface name */
134 char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
135 char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
138 static struct sockaddr_in mcast_addr;
141 static inline void sb_queue_tail(struct ip_vs_sync_buff *sb)
143 spin_lock(&ip_vs_sync_lock);
144 list_add_tail(&sb->list, &ip_vs_sync_queue);
145 spin_unlock(&ip_vs_sync_lock);
148 static inline struct ip_vs_sync_buff * sb_dequeue(void)
150 struct ip_vs_sync_buff *sb;
152 spin_lock_bh(&ip_vs_sync_lock);
153 if (list_empty(&ip_vs_sync_queue)) {
156 sb = list_entry(ip_vs_sync_queue.next,
157 struct ip_vs_sync_buff,
161 spin_unlock_bh(&ip_vs_sync_lock);
166 static inline struct ip_vs_sync_buff * ip_vs_sync_buff_create(void)
168 struct ip_vs_sync_buff *sb;
170 if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
173 if (!(sb->mesg=kmalloc(sync_send_mesg_maxlen, GFP_ATOMIC))) {
177 sb->mesg->nr_conns = 0;
178 sb->mesg->syncid = ip_vs_master_syncid;
180 sb->head = (unsigned char *)sb->mesg + 4;
181 sb->end = (unsigned char *)sb->mesg + sync_send_mesg_maxlen;
182 sb->firstuse = jiffies;
186 static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff *sb)
193 * Get the current sync buffer if it has been created for more
194 * than the specified time or the specified time is zero.
196 static inline struct ip_vs_sync_buff *
197 get_curr_sync_buff(unsigned long time)
199 struct ip_vs_sync_buff *sb;
201 spin_lock_bh(&curr_sb_lock);
202 if (curr_sb && (time == 0 ||
203 time_before(jiffies - curr_sb->firstuse, time))) {
208 spin_unlock_bh(&curr_sb_lock);
214 * Add an ip_vs_conn information into the current sync_buff.
215 * Called by ip_vs_in.
217 void ip_vs_sync_conn(struct ip_vs_conn *cp)
219 struct ip_vs_sync_mesg *m;
220 struct ip_vs_sync_conn *s;
223 spin_lock(&curr_sb_lock);
225 if (!(curr_sb=ip_vs_sync_buff_create())) {
226 spin_unlock(&curr_sb_lock);
227 IP_VS_ERR("ip_vs_sync_buff_create failed.\n");
232 len = (cp->flags & IP_VS_CONN_F_SEQ_MASK) ? FULL_CONN_SIZE :
235 s = (struct ip_vs_sync_conn *)curr_sb->head;
238 s->protocol = cp->protocol;
239 s->cport = cp->cport;
240 s->vport = cp->vport;
241 s->dport = cp->dport;
242 s->caddr = cp->caddr;
243 s->vaddr = cp->vaddr;
244 s->daddr = cp->daddr;
245 s->flags = htons(cp->flags & ~IP_VS_CONN_F_HASHED);
246 s->state = htons(cp->state);
247 if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
248 struct ip_vs_sync_conn_options *opt =
249 (struct ip_vs_sync_conn_options *)&s[1];
250 memcpy(opt, &cp->in_seq, sizeof(*opt));
255 curr_sb->head += len;
257 /* check if there is a space for next one */
258 if (curr_sb->head+FULL_CONN_SIZE > curr_sb->end) {
259 sb_queue_tail(curr_sb);
262 spin_unlock(&curr_sb_lock);
264 /* synchronize its controller if it has */
266 ip_vs_sync_conn(cp->control);
271 * Process received multicast message and create the corresponding
272 * ip_vs_conn entries.
274 static void ip_vs_process_message(const char *buffer, const size_t buflen)
276 struct ip_vs_sync_mesg *m = (struct ip_vs_sync_mesg *)buffer;
277 struct ip_vs_sync_conn *s;
278 struct ip_vs_sync_conn_options *opt;
279 struct ip_vs_conn *cp;
283 /* Convert size back to host byte order */
284 m->size = ntohs(m->size);
286 if (buflen != m->size) {
287 IP_VS_ERR("bogus message\n");
291 /* SyncID sanity check */
292 if (ip_vs_backup_syncid != 0 && m->syncid != ip_vs_backup_syncid) {
293 IP_VS_DBG(7, "Ignoring incoming msg with syncid = %d\n",
298 p = (char *)buffer + sizeof(struct ip_vs_sync_mesg);
299 for (i=0; i<m->nr_conns; i++) {
302 s = (struct ip_vs_sync_conn *)p;
303 flags = ntohs(s->flags);
304 if (!(flags & IP_VS_CONN_F_TEMPLATE))
305 cp = ip_vs_conn_in_get(s->protocol,
309 cp = ip_vs_ct_in_get(s->protocol,
313 cp = ip_vs_conn_new(s->protocol,
319 IP_VS_ERR("ip_vs_conn_new failed\n");
322 cp->state = ntohs(s->state);
323 } else if (!cp->dest) {
324 /* it is an entry created by the synchronization */
325 cp->state = ntohs(s->state);
326 cp->flags = flags | IP_VS_CONN_F_HASHED;
327 } /* Note that we don't touch its state and flags
328 if it is a normal entry. */
330 if (flags & IP_VS_CONN_F_SEQ_MASK) {
331 opt = (struct ip_vs_sync_conn_options *)&s[1];
332 memcpy(&cp->in_seq, opt, sizeof(*opt));
335 p += SIMPLE_CONN_SIZE;
337 atomic_set(&cp->in_pkts, sysctl_ip_vs_sync_threshold[0]);
338 cp->timeout = IP_VS_SYNC_CONN_TIMEOUT;
341 if (p > buffer+buflen) {
342 IP_VS_ERR("bogus message\n");
350 * Setup loopback of outgoing multicasts on a sending socket
352 static void set_mcast_loop(struct sock *sk, u_char loop)
354 struct inet_sock *inet = inet_sk(sk);
356 /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
358 inet->mc_loop = loop ? 1 : 0;
363 * Specify TTL for outgoing multicasts on a sending socket
365 static void set_mcast_ttl(struct sock *sk, u_char ttl)
367 struct inet_sock *inet = inet_sk(sk);
369 /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
376 * Specifiy default interface for outgoing multicasts
378 static int set_mcast_if(struct sock *sk, char *ifname)
380 struct net_device *dev;
381 struct inet_sock *inet = inet_sk(sk);
383 if ((dev = __dev_get_by_name(ifname)) == NULL)
386 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
390 inet->mc_index = dev->ifindex;
391 /* inet->mc_addr = 0; */
399 * Set the maximum length of sync message according to the
400 * specified interface's MTU.
402 static int set_sync_mesg_maxlen(int sync_state)
404 struct net_device *dev;
407 if (sync_state == IP_VS_STATE_MASTER) {
408 if ((dev = __dev_get_by_name(ip_vs_master_mcast_ifn)) == NULL)
411 num = (dev->mtu - sizeof(struct iphdr) -
412 sizeof(struct udphdr) -
413 SYNC_MESG_HEADER_LEN - 20) / SIMPLE_CONN_SIZE;
414 sync_send_mesg_maxlen =
415 SYNC_MESG_HEADER_LEN + SIMPLE_CONN_SIZE * num;
416 IP_VS_DBG(7, "setting the maximum length of sync sending "
417 "message %d.\n", sync_send_mesg_maxlen);
418 } else if (sync_state == IP_VS_STATE_BACKUP) {
419 if ((dev = __dev_get_by_name(ip_vs_backup_mcast_ifn)) == NULL)
422 sync_recv_mesg_maxlen = dev->mtu -
423 sizeof(struct iphdr) - sizeof(struct udphdr);
424 IP_VS_DBG(7, "setting the maximum length of sync receiving "
425 "message %d.\n", sync_recv_mesg_maxlen);
433 * Join a multicast group.
434 * the group is specified by a class D multicast address 224.0.0.0/8
435 * in the in_addr structure passed in as a parameter.
438 join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
440 struct ip_mreqn mreq;
441 struct net_device *dev;
444 memset(&mreq, 0, sizeof(mreq));
445 memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
447 if ((dev = __dev_get_by_name(ifname)) == NULL)
449 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
452 mreq.imr_ifindex = dev->ifindex;
455 ret = ip_mc_join_group(sk, &mreq);
462 static int bind_mcastif_addr(struct socket *sock, char *ifname)
464 struct net_device *dev;
466 struct sockaddr_in sin;
468 if ((dev = __dev_get_by_name(ifname)) == NULL)
471 addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
473 IP_VS_ERR("You probably need to specify IP address on "
474 "multicast interface.\n");
476 IP_VS_DBG(7, "binding socket with (%s) %u.%u.%u.%u\n",
477 ifname, NIPQUAD(addr));
479 /* Now bind the socket with the address of multicast interface */
480 sin.sin_family = AF_INET;
481 sin.sin_addr.s_addr = addr;
484 return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
488 * Set up sending multicast socket over UDP
490 static struct socket * make_send_sock(void)
494 /* First create a socket */
495 if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
496 IP_VS_ERR("Error during creation of socket; terminating\n");
500 if (set_mcast_if(sock->sk, ip_vs_master_mcast_ifn) < 0) {
501 IP_VS_ERR("Error setting outbound mcast interface\n");
505 set_mcast_loop(sock->sk, 0);
506 set_mcast_ttl(sock->sk, 1);
508 if (bind_mcastif_addr(sock, ip_vs_master_mcast_ifn) < 0) {
509 IP_VS_ERR("Error binding address of the mcast interface\n");
513 if (sock->ops->connect(sock,
514 (struct sockaddr*)&mcast_addr,
515 sizeof(struct sockaddr), 0) < 0) {
516 IP_VS_ERR("Error connecting to the multicast addr\n");
529 * Set up receiving multicast socket over UDP
531 static struct socket * make_receive_sock(void)
535 /* First create a socket */
536 if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
537 IP_VS_ERR("Error during creation of socket; terminating\n");
541 /* it is equivalent to the REUSEADDR option in user-space */
542 sock->sk->sk_reuse = 1;
544 if (sock->ops->bind(sock,
545 (struct sockaddr*)&mcast_addr,
546 sizeof(struct sockaddr)) < 0) {
547 IP_VS_ERR("Error binding to the multicast addr\n");
551 /* join the multicast group */
552 if (join_mcast_group(sock->sk,
553 (struct in_addr*)&mcast_addr.sin_addr,
554 ip_vs_backup_mcast_ifn) < 0) {
555 IP_VS_ERR("Error joining to the multicast group\n");
568 ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
570 struct msghdr msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
575 iov.iov_base = (void *)buffer;
576 iov.iov_len = length;
578 len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
585 ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
591 /* Put size in network byte order */
592 msg->size = htons(msg->size);
594 if (ip_vs_send_async(sock, (char *)msg, msize) != msize)
595 IP_VS_ERR("ip_vs_send_async error\n");
599 ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
601 struct msghdr msg = {NULL,};
607 /* Receive a packet */
608 iov.iov_base = buffer;
609 iov.iov_len = (size_t)buflen;
611 len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, 0);
621 static DECLARE_WAIT_QUEUE_HEAD(sync_wait);
622 static pid_t sync_master_pid = 0;
623 static pid_t sync_backup_pid = 0;
625 static DECLARE_WAIT_QUEUE_HEAD(stop_sync_wait);
626 static int stop_master_sync = 0;
627 static int stop_backup_sync = 0;
629 static void sync_master_loop(void)
632 struct ip_vs_sync_buff *sb;
634 /* create the sending multicast socket */
635 sock = make_send_sock();
639 IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, "
641 ip_vs_master_mcast_ifn, ip_vs_master_syncid);
644 while ((sb=sb_dequeue())) {
645 ip_vs_send_sync_msg(sock, sb->mesg);
646 ip_vs_sync_buff_release(sb);
649 /* check if entries stay in curr_sb for 2 seconds */
650 if ((sb = get_curr_sync_buff(2*HZ))) {
651 ip_vs_send_sync_msg(sock, sb->mesg);
652 ip_vs_sync_buff_release(sb);
655 if (stop_master_sync)
661 /* clean up the sync_buff queue */
662 while ((sb=sb_dequeue())) {
663 ip_vs_sync_buff_release(sb);
666 /* clean up the current sync_buff */
667 if ((sb = get_curr_sync_buff(0))) {
668 ip_vs_sync_buff_release(sb);
671 /* release the sending multicast socket */
676 static void sync_backup_loop(void)
682 if (!(buf = kmalloc(sync_recv_mesg_maxlen, GFP_ATOMIC))) {
683 IP_VS_ERR("sync_backup_loop: kmalloc error\n");
687 /* create the receiving multicast socket */
688 sock = make_receive_sock();
692 IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
694 ip_vs_backup_mcast_ifn, ip_vs_backup_syncid);
697 /* do you have data now? */
698 while (!skb_queue_empty(&(sock->sk->sk_receive_queue))) {
700 ip_vs_receive(sock, buf,
701 sync_recv_mesg_maxlen)) <= 0) {
702 IP_VS_ERR("receiving message error\n");
705 /* disable bottom half, because it accessed the data
706 shared by softirq while getting/creating conns */
708 ip_vs_process_message(buf, len);
712 if (stop_backup_sync)
718 /* release the sending multicast socket */
726 static void set_sync_pid(int sync_state, pid_t sync_pid)
728 if (sync_state == IP_VS_STATE_MASTER)
729 sync_master_pid = sync_pid;
730 else if (sync_state == IP_VS_STATE_BACKUP)
731 sync_backup_pid = sync_pid;
734 static void set_stop_sync(int sync_state, int set)
736 if (sync_state == IP_VS_STATE_MASTER)
737 stop_master_sync = set;
738 else if (sync_state == IP_VS_STATE_BACKUP)
739 stop_backup_sync = set;
741 stop_master_sync = set;
742 stop_backup_sync = set;
746 static int sync_thread(void *startup)
748 DECLARE_WAITQUEUE(wait, current);
753 /* increase the module use count */
754 ip_vs_use_count_inc();
756 if (ip_vs_sync_state & IP_VS_STATE_MASTER && !sync_master_pid) {
757 state = IP_VS_STATE_MASTER;
758 name = "ipvs_syncmaster";
759 } else if (ip_vs_sync_state & IP_VS_STATE_BACKUP && !sync_backup_pid) {
760 state = IP_VS_STATE_BACKUP;
761 name = "ipvs_syncbackup";
764 ip_vs_use_count_dec();
773 /* Block all signals */
774 spin_lock_irq(¤t->sighand->siglock);
775 siginitsetinv(¤t->blocked, 0);
777 spin_unlock_irq(¤t->sighand->siglock);
779 /* set the maximum length of sync message */
780 set_sync_mesg_maxlen(state);
782 /* set up multicast address */
783 mcast_addr.sin_family = AF_INET;
784 mcast_addr.sin_port = htons(IP_VS_SYNC_PORT);
785 mcast_addr.sin_addr.s_addr = htonl(IP_VS_SYNC_GROUP);
787 add_wait_queue(&sync_wait, &wait);
789 set_sync_pid(state, current->pid);
790 complete((struct completion *)startup);
792 /* processing master/backup loop here */
793 if (state == IP_VS_STATE_MASTER)
795 else if (state == IP_VS_STATE_BACKUP)
799 remove_wait_queue(&sync_wait, &wait);
802 set_sync_pid(state, 0);
803 IP_VS_INFO("sync thread stopped!\n");
807 /* decrease the module use count */
808 ip_vs_use_count_dec();
810 set_stop_sync(state, 0);
811 wake_up(&stop_sync_wait);
817 static int fork_sync_thread(void *startup)
821 /* fork the sync thread here, then the parent process of the
822 sync thread is the init process after this thread exits. */
824 if ((pid = kernel_thread(sync_thread, startup, 0)) < 0) {
825 IP_VS_ERR("could not create sync_thread due to %d... "
835 int start_sync_thread(int state, char *mcast_ifn, __u8 syncid)
837 DECLARE_COMPLETION(startup);
840 if ((state == IP_VS_STATE_MASTER && sync_master_pid) ||
841 (state == IP_VS_STATE_BACKUP && sync_backup_pid))
844 IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, current->pid);
845 IP_VS_DBG(7, "Each ip_vs_sync_conn entry need %Zd bytes\n",
846 sizeof(struct ip_vs_sync_conn));
848 ip_vs_sync_state |= state;
849 if (state == IP_VS_STATE_MASTER) {
850 strlcpy(ip_vs_master_mcast_ifn, mcast_ifn, sizeof(ip_vs_master_mcast_ifn));
851 ip_vs_master_syncid = syncid;
853 strlcpy(ip_vs_backup_mcast_ifn, mcast_ifn, sizeof(ip_vs_backup_mcast_ifn));
854 ip_vs_backup_syncid = syncid;
858 if ((pid = kernel_thread(fork_sync_thread, &startup, 0)) < 0) {
859 IP_VS_ERR("could not create fork_sync_thread due to %d... "
865 wait_for_completion(&startup);
871 int stop_sync_thread(int state)
873 DECLARE_WAITQUEUE(wait, current);
875 if ((state == IP_VS_STATE_MASTER && !sync_master_pid) ||
876 (state == IP_VS_STATE_BACKUP && !sync_backup_pid))
879 IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, current->pid);
880 IP_VS_INFO("stopping sync thread %d ...\n",
881 (state == IP_VS_STATE_MASTER) ? sync_master_pid : sync_backup_pid);
883 __set_current_state(TASK_UNINTERRUPTIBLE);
884 add_wait_queue(&stop_sync_wait, &wait);
885 set_stop_sync(state, 1);
886 ip_vs_sync_state -= state;
889 __set_current_state(TASK_RUNNING);
890 remove_wait_queue(&stop_sync_wait, &wait);
892 /* Note: no need to reap the sync thread, because its parent
893 process is the init process */
895 if ((state == IP_VS_STATE_MASTER && stop_master_sync) ||
896 (state == IP_VS_STATE_BACKUP && stop_backup_sync))