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.8 2002/08/17 14:06:02 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>
26 #include <linux/skbuff.h>
28 #include <linux/igmp.h> /* for ip_mc_join_group */
32 #include <asm/uaccess.h> /* for get_fs and set_fs */
34 #include <net/ip_vs.h>
36 #define IP_VS_SYNC_GROUP 0xe0000051 /* multicast addr - 224.0.0.81 */
37 #define IP_VS_SYNC_PORT 8848 /* multicast port */
41 * IPVS sync connection entry
43 struct ip_vs_sync_conn {
46 /* Protocol, addresses and port numbers */
47 __u8 protocol; /* Which protocol (TCP/UDP) */
51 __u32 caddr; /* client address */
52 __u32 vaddr; /* virtual address */
53 __u32 daddr; /* destination address */
55 /* Flags and state transition */
56 __u16 flags; /* status flags */
57 __u16 state; /* state info */
59 /* The sequence options start here */
62 struct ip_vs_sync_conn_options {
63 struct ip_vs_seq in_seq; /* incoming seq. struct */
64 struct ip_vs_seq out_seq; /* outgoing seq. struct */
67 #define IP_VS_SYNC_CONN_TIMEOUT (3*60*HZ)
68 #define SIMPLE_CONN_SIZE (sizeof(struct ip_vs_sync_conn))
69 #define FULL_CONN_SIZE \
70 (sizeof(struct ip_vs_sync_conn) + sizeof(struct ip_vs_sync_conn_options))
74 The master mulitcasts messages to the backup load balancers in the
78 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
79 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
80 | Count Conns | Sync ID | Size |
81 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
83 | IPVS Sync Connection (1) |
84 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
88 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90 | IPVS Sync Connection (n) |
91 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93 Count Conns : Number of IPVS sync Connection entries.
94 Sync ID : IPVS sync group we belong to.
95 Size : Size of packet.
98 #define SYNC_MESG_MAX_SIZE (24*50+4)
99 struct ip_vs_sync_mesg {
104 /* ip_vs_sync_conn entries start here */
108 struct ip_vs_sync_buff {
109 struct list_head list;
110 unsigned long firstuse;
112 /* pointers for the message data */
113 struct ip_vs_sync_mesg *mesg;
119 /* the sync_buff list head and the lock */
120 static LIST_HEAD(ip_vs_sync_queue);
121 static spinlock_t ip_vs_sync_lock = SPIN_LOCK_UNLOCKED;
123 /* current sync_buff for accepting new conn entries */
124 static struct ip_vs_sync_buff *curr_sb = NULL;
125 static spinlock_t curr_sb_lock = SPIN_LOCK_UNLOCKED;
127 /* ipvs sync daemon state */
128 volatile int ip_vs_sync_state = IP_VS_STATE_NONE;
129 volatile int ip_vs_master_syncid = 0;
130 volatile int ip_vs_backup_syncid = 0;
132 /* multicast interface name */
133 char ip_vs_mcast_master_ifn[IP_VS_IFNAME_MAXLEN];
134 char ip_vs_mcast_backup_ifn[IP_VS_IFNAME_MAXLEN];
137 static struct sockaddr_in mcast_addr;
139 static inline void sb_queue_tail(struct ip_vs_sync_buff *sb)
141 spin_lock(&ip_vs_sync_lock);
142 list_add_tail(&sb->list, &ip_vs_sync_queue);
143 spin_unlock(&ip_vs_sync_lock);
146 static inline struct ip_vs_sync_buff * sb_dequeue(void)
148 struct ip_vs_sync_buff *sb;
150 spin_lock_bh(&ip_vs_sync_lock);
151 if (list_empty(&ip_vs_sync_queue)) {
154 sb = list_entry(ip_vs_sync_queue.next,
155 struct ip_vs_sync_buff,
159 spin_unlock_bh(&ip_vs_sync_lock);
164 static inline struct ip_vs_sync_buff * ip_vs_sync_buff_create(void)
166 struct ip_vs_sync_buff *sb;
168 if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
171 if (!(sb->mesg=kmalloc(SYNC_MESG_MAX_SIZE, GFP_ATOMIC))) {
175 sb->mesg->nr_conns = 0;
176 sb->mesg->syncid = ip_vs_master_syncid;
178 sb->head = (unsigned char *)sb->mesg + 4;
179 sb->end = (unsigned char *)sb->mesg + SYNC_MESG_MAX_SIZE;
180 sb->firstuse = jiffies;
184 static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff *sb)
191 * Get the current sync buffer if it has been created for more
192 * than the specified time or the specified time is zero.
194 static inline struct ip_vs_sync_buff *
195 get_curr_sync_buff(unsigned long time)
197 struct ip_vs_sync_buff *sb;
199 spin_lock_bh(&curr_sb_lock);
201 (jiffies - curr_sb->firstuse > time || time == 0)) {
206 spin_unlock_bh(&curr_sb_lock);
212 * Add an ip_vs_conn information into the current sync_buff.
213 * Called by ip_vs_in.
215 void ip_vs_sync_conn(struct ip_vs_conn *cp)
217 struct ip_vs_sync_mesg *m;
218 struct ip_vs_sync_conn *s;
221 spin_lock(&curr_sb_lock);
223 if (!(curr_sb=ip_vs_sync_buff_create())) {
224 spin_unlock(&curr_sb_lock);
225 IP_VS_ERR("ip_vs_sync_buff_create failed.\n");
230 len = (cp->flags & IP_VS_CONN_F_SEQ_MASK) ? FULL_CONN_SIZE :
233 s = (struct ip_vs_sync_conn *)curr_sb->head;
236 s->protocol = cp->protocol;
237 s->cport = cp->cport;
238 s->vport = cp->vport;
239 s->dport = cp->dport;
240 s->caddr = cp->caddr;
241 s->vaddr = cp->vaddr;
242 s->daddr = cp->daddr;
243 s->flags = htons(cp->flags & ~IP_VS_CONN_F_HASHED);
244 s->state = htons(cp->state);
245 if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
246 struct ip_vs_sync_conn_options *opt =
247 (struct ip_vs_sync_conn_options *)&s[1];
248 memcpy(opt, &cp->in_seq, sizeof(*opt));
253 curr_sb->head += len;
255 /* check if there is a space for next one */
256 if (curr_sb->head+FULL_CONN_SIZE > curr_sb->end) {
257 sb_queue_tail(curr_sb);
260 spin_unlock(&curr_sb_lock);
262 /* synchronize its controller if it has */
264 ip_vs_sync_conn(cp->control);
269 * Process received multicast message and create the corresponding
270 * ip_vs_conn entries.
272 static void ip_vs_process_message(const char *buffer, const size_t buflen)
274 struct ip_vs_sync_mesg *m = (struct ip_vs_sync_mesg *)buffer;
275 struct ip_vs_sync_conn *s;
276 struct ip_vs_sync_conn_options *opt;
277 struct ip_vs_conn *cp;
281 /* Convert size back to host byte order */
282 m->size = ntohs(m->size);
284 if (buflen != m->size) {
285 IP_VS_ERR("bogus message\n");
289 /* SyncID sanity check */
290 if (ip_vs_backup_syncid != 0 && m->syncid != ip_vs_backup_syncid) {
291 IP_VS_DBG(7, "Ignoring incoming msg with syncid = %d\n",
296 p = (char *)buffer + sizeof(struct ip_vs_sync_mesg);
297 for (i=0; i<m->nr_conns; i++) {
300 s = (struct ip_vs_sync_conn *)p;
301 flags = ntohs(s->flags);
302 if (!(flags & IP_VS_CONN_F_TEMPLATE))
303 cp = ip_vs_conn_in_get(s->protocol,
307 cp = ip_vs_ct_in_get(s->protocol,
311 cp = ip_vs_conn_new(s->protocol,
317 IP_VS_ERR("ip_vs_conn_new failed\n");
320 cp->state = ntohs(s->state);
321 } else if (!cp->dest) {
322 /* it is an entry created by the synchronization */
323 cp->state = ntohs(s->state);
324 cp->flags = flags | IP_VS_CONN_F_HASHED;
325 } /* Note that we don't touch its state and flags
326 if it is a normal entry. */
328 if (flags & IP_VS_CONN_F_SEQ_MASK) {
329 opt = (struct ip_vs_sync_conn_options *)&s[1];
330 memcpy(&cp->in_seq, opt, sizeof(*opt));
333 p += SIMPLE_CONN_SIZE;
335 atomic_set(&cp->in_pkts, sysctl_ip_vs_sync_threshold);
336 cp->timeout = IP_VS_SYNC_CONN_TIMEOUT;
339 if (p > buffer+buflen) {
340 IP_VS_ERR("bogus message\n");
348 * Setup loopback of outgoing multicasts on a sending socket
350 static void set_mcast_loop(struct sock *sk, u_char loop)
352 /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
354 sk->protinfo.af_inet.mc_loop = loop ? 1 : 0;
359 * Specify TTL for outgoing multicasts on a sending socket
361 static void set_mcast_ttl(struct sock *sk, u_char ttl)
363 /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
365 sk->protinfo.af_inet.mc_ttl = ttl;
370 * Specifiy default interface for outgoing multicasts
372 static int set_mcast_if(struct sock *sk, char *ifname)
374 struct net_device *dev;
376 if ((dev = __dev_get_by_name(ifname)) == NULL)
379 if (sk->bound_dev_if && dev->ifindex != sk->bound_dev_if)
383 sk->protinfo.af_inet.mc_index = dev->ifindex;
384 /* sk->protinfo.af_inet.mc_addr = 0; */
391 * Join a multicast group.
392 * the group is specified by a class D multicast address 224.0.0.0/8
393 * in the in_addr structure passed in as a parameter.
396 join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
398 struct ip_mreqn mreq;
399 struct net_device *dev;
402 memset(&mreq, 0, sizeof(mreq));
403 memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
405 if ((dev = __dev_get_by_name(ifname)) == NULL)
407 if (sk->bound_dev_if && dev->ifindex != sk->bound_dev_if)
410 mreq.imr_ifindex = dev->ifindex;
413 ret = ip_mc_join_group(sk, &mreq);
420 static int bind_mcastif_addr(struct socket *sock, char *ifname)
422 struct net_device *dev;
424 struct sockaddr_in sin;
426 if ((dev = __dev_get_by_name(ifname)) == NULL)
429 addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
431 IP_VS_ERR("You probably need to specify IP address on "
432 "multicast interface.\n");
434 IP_VS_DBG(7, "binding socket with (%s) %u.%u.%u.%u\n",
435 ifname, NIPQUAD(addr));
437 /* Now bind the socket with the address of multicast interface */
438 sin.sin_family = AF_INET;
439 sin.sin_addr.s_addr = addr;
442 return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
446 * Set up sending multicast socket over UDP
448 static struct socket * make_send_sock(void)
452 /* First create a socket */
453 if (sock_create(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
454 IP_VS_ERR("Error during creation of socket; terminating\n");
458 if (set_mcast_if(sock->sk, ip_vs_mcast_master_ifn) < 0) {
459 IP_VS_ERR("Error setting outbound mcast interface\n");
463 set_mcast_loop(sock->sk, 0);
464 set_mcast_ttl(sock->sk, 1);
466 if (bind_mcastif_addr(sock, ip_vs_mcast_master_ifn) < 0) {
467 IP_VS_ERR("Error binding address of the mcast interface\n");
471 if (sock->ops->connect(sock,
472 (struct sockaddr*)&mcast_addr,
473 sizeof(struct sockaddr), 0) < 0) {
474 IP_VS_ERR("Error connecting to the multicast addr\n");
487 * Set up receiving multicast socket over UDP
489 static struct socket * make_receive_sock(void)
493 /* First create a socket */
494 if (sock_create(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
495 IP_VS_ERR("Error during creation of socket; terminating\n");
499 /* it is equivalent to the REUSEADDR option in user-space */
502 if (sock->ops->bind(sock,
503 (struct sockaddr*)&mcast_addr,
504 sizeof(struct sockaddr)) < 0) {
505 IP_VS_ERR("Error binding to the multicast addr\n");
509 /* join the multicast group */
510 if (join_mcast_group(sock->sk,
511 (struct in_addr*)&mcast_addr.sin_addr,
512 ip_vs_mcast_backup_ifn) < 0) {
513 IP_VS_ERR("Error joining to the multicast group\n");
526 ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
534 iov.iov_base = (void *)buffer;
535 iov.iov_len = length;
540 msg.msg_control = NULL;
541 msg.msg_controllen = 0;
542 msg.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL;
544 oldfs = get_fs(); set_fs(KERNEL_DS);
545 len = sock_sendmsg(sock, &msg, (size_t)(length));
553 ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
559 /* Put size in network byte order */
560 msg->size = htons(msg->size);
562 if (ip_vs_send_async(sock, (char *)msg, msize) != msize)
563 IP_VS_ERR("ip_vs_send_async error\n");
567 ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
576 /* Receive a packet */
577 iov.iov_base = buffer;
578 iov.iov_len = (size_t)buflen;
583 msg.msg_control = NULL;
584 msg.msg_controllen = 0;
587 oldfs = get_fs(); set_fs(KERNEL_DS);
588 len = sock_recvmsg(sock, &msg, buflen, 0);
599 static DECLARE_WAIT_QUEUE_HEAD(sync_wait);
600 static pid_t sync_master_pid = 0;
601 static pid_t sync_backup_pid = 0;
603 static DECLARE_WAIT_QUEUE_HEAD(stop_sync_wait);
604 static int stop_master_sync = 0;
605 static int stop_backup_sync = 0;
607 static void sync_master_loop(void)
610 struct ip_vs_sync_buff *sb;
612 /* create the sending multicast socket */
613 sock = make_send_sock();
617 IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, "
619 ip_vs_mcast_master_ifn, ip_vs_master_syncid);
622 while ((sb=sb_dequeue())) {
623 ip_vs_send_sync_msg(sock, sb->mesg);
624 ip_vs_sync_buff_release(sb);
627 /* check if entries stay in curr_sb for 2 seconds */
628 if ((sb = get_curr_sync_buff(2*HZ))) {
629 ip_vs_send_sync_msg(sock, sb->mesg);
630 ip_vs_sync_buff_release(sb);
633 if (stop_master_sync)
636 __set_current_state(TASK_INTERRUPTIBLE);
637 schedule_timeout(HZ);
638 __set_current_state(TASK_RUNNING);
641 /* clean up the sync_buff queue */
642 while ((sb=sb_dequeue())) {
643 ip_vs_sync_buff_release(sb);
646 /* clean up the current sync_buff */
647 if ((sb = get_curr_sync_buff(0))) {
648 ip_vs_sync_buff_release(sb);
651 /* release the sending multicast socket */
656 static void sync_backup_loop(void)
662 if (!(buf=kmalloc(SYNC_MESG_MAX_SIZE, GFP_ATOMIC))) {
663 IP_VS_ERR("sync_backup_loop: kmalloc error\n");
667 /* create the receiving multicast socket */
668 sock = make_receive_sock();
672 IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
674 ip_vs_mcast_backup_ifn, ip_vs_backup_syncid);
677 /* do you have data now? */
678 while (!skb_queue_empty(&(sock->sk->receive_queue))) {
679 if ((len=ip_vs_receive(sock, buf,
680 SYNC_MESG_MAX_SIZE))<=0) {
681 IP_VS_ERR("receiving message error\n");
684 /* disable bottom half, because it accessed the data
685 shared by softirq while getting/creating conns */
687 ip_vs_process_message(buf, len);
691 if (stop_backup_sync)
694 __set_current_state(TASK_INTERRUPTIBLE);
695 schedule_timeout(HZ);
696 __set_current_state(TASK_RUNNING);
699 /* release the sending multicast socket */
706 static void sync_pid_set(int sync_state, pid_t sync_pid)
708 if (sync_state == IP_VS_STATE_MASTER)
709 sync_master_pid = sync_pid;
710 else if (sync_state == IP_VS_STATE_BACKUP)
711 sync_backup_pid = sync_pid;
714 static void sync_stop_set(int sync_state, int set)
716 if (sync_state == IP_VS_STATE_MASTER)
717 stop_master_sync = set;
718 else if (sync_state == IP_VS_STATE_BACKUP)
719 stop_backup_sync = set;
721 stop_master_sync = set;
722 stop_backup_sync = set;
726 static int sync_thread(void *startup)
728 DECLARE_WAITQUEUE(wait, current);
730 int state = IP_VS_STATE_NONE;
738 if (ip_vs_sync_state & IP_VS_STATE_MASTER && !sync_master_pid) {
739 state = IP_VS_STATE_MASTER;
740 sprintf(current->comm, "ipvs_syncmaster");
741 } else if (ip_vs_sync_state & IP_VS_STATE_BACKUP) {
742 state = IP_VS_STATE_BACKUP;
743 sprintf(current->comm, "ipvs_syncbackup");
746 /* Block all signals */
747 spin_lock_irq(¤t->sigmask_lock);
748 siginitsetinv(¤t->blocked, 0);
749 recalc_sigpending(current);
750 spin_unlock_irq(¤t->sigmask_lock);
752 /* set up multicast address */
753 mcast_addr.sin_family = AF_INET;
754 mcast_addr.sin_port = htons(IP_VS_SYNC_PORT);
755 mcast_addr.sin_addr.s_addr = htonl(IP_VS_SYNC_GROUP);
757 add_wait_queue(&sync_wait, &wait);
759 sync_pid_set(state, current->pid);
760 complete((struct completion *)startup);
762 /* processing master/backup loop here */
763 if (state == IP_VS_STATE_MASTER)
765 else if (state == IP_VS_STATE_BACKUP)
769 remove_wait_queue(&sync_wait, &wait);
772 sync_pid_set(state, 0);
773 IP_VS_INFO("sync thread stopped!\n");
778 sync_stop_set(state, 0);
779 wake_up(&stop_sync_wait);
785 static int fork_sync_thread(void *startup)
789 /* fork the sync thread here, then the parent process of the
790 sync thread is the init process after this thread exits. */
792 if ((pid = kernel_thread(sync_thread, startup, 0)) < 0) {
793 IP_VS_ERR("could not create sync_thread due to %d... "
795 current->state = TASK_UNINTERRUPTIBLE;
796 schedule_timeout(HZ);
804 int start_sync_thread(int state, char *mcast_ifn, __u8 syncid)
806 DECLARE_COMPLETION(startup);
809 if ((state == IP_VS_STATE_MASTER && sync_master_pid) ||
810 (state == IP_VS_STATE_BACKUP && sync_backup_pid))
813 IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, current->pid);
814 IP_VS_DBG(7, "Each ip_vs_sync_conn entry need %d bytes\n",
815 sizeof(struct ip_vs_sync_conn));
817 ip_vs_sync_state |= state;
818 if (state == IP_VS_STATE_MASTER) {
819 strncpy(ip_vs_mcast_master_ifn, mcast_ifn, sizeof(ip_vs_mcast_master_ifn));
820 ip_vs_mcast_master_ifn[sizeof(ip_vs_mcast_master_ifn) - 1] = 0;
821 ip_vs_master_syncid = syncid;
823 strncpy(ip_vs_mcast_backup_ifn, mcast_ifn, sizeof(ip_vs_mcast_backup_ifn));
824 ip_vs_mcast_backup_ifn[sizeof(ip_vs_mcast_backup_ifn) - 1] = 0;
825 ip_vs_backup_syncid = syncid;
829 if ((pid = kernel_thread(fork_sync_thread, &startup, 0)) < 0) {
830 IP_VS_ERR("could not create fork_sync_thread due to %d... "
832 current->state = TASK_UNINTERRUPTIBLE;
833 schedule_timeout(HZ);
837 wait_for_completion(&startup);
843 int stop_sync_thread(int state)
845 DECLARE_WAITQUEUE(wait, current);
847 if ((state == IP_VS_STATE_MASTER && !sync_master_pid) ||
848 (state == IP_VS_STATE_BACKUP && !sync_backup_pid))
851 IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, current->pid);
852 IP_VS_INFO("stopping sync thread %d ...\n",
853 (state == IP_VS_STATE_MASTER) ? sync_master_pid : sync_backup_pid);
855 __set_current_state(TASK_UNINTERRUPTIBLE);
856 add_wait_queue(&stop_sync_wait, &wait);
857 sync_stop_set(state, 1);
858 ip_vs_sync_state -= state;
861 __set_current_state(TASK_RUNNING);
862 remove_wait_queue(&stop_sync_wait, &wait);
864 /* Note: no need to reap the sync thread, because its parent
865 process is the init process */
867 if ((state == IP_VS_STATE_MASTER && stop_master_sync) ||
868 (state == IP_VS_STATE_BACKUP && stop_backup_sync))