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++) {
300 s = (struct ip_vs_sync_conn *)p;
301 cp = ip_vs_conn_in_get(s->protocol,
305 cp = ip_vs_conn_new(s->protocol,
309 ntohs(s->flags), NULL);
311 IP_VS_ERR("ip_vs_conn_new failed\n");
314 cp->state = ntohs(s->state);
315 } else if (!cp->dest) {
316 /* it is an entry created by the synchronization */
317 cp->state = ntohs(s->state);
318 cp->flags = ntohs(s->flags) | IP_VS_CONN_F_HASHED;
319 } /* Note that we don't touch its state and flags
320 if it is a normal entry. */
322 if (ntohs(s->flags) & IP_VS_CONN_F_SEQ_MASK) {
323 opt = (struct ip_vs_sync_conn_options *)&s[1];
324 memcpy(&cp->in_seq, opt, sizeof(*opt));
327 p += SIMPLE_CONN_SIZE;
329 atomic_set(&cp->in_pkts, sysctl_ip_vs_sync_threshold[0]);
330 cp->timeout = IP_VS_SYNC_CONN_TIMEOUT;
333 if (p > buffer+buflen) {
334 IP_VS_ERR("bogus message\n");
342 * Setup loopback of outgoing multicasts on a sending socket
344 static void set_mcast_loop(struct sock *sk, u_char loop)
346 struct inet_sock *inet = inet_sk(sk);
348 /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
350 inet->mc_loop = loop ? 1 : 0;
355 * Specify TTL for outgoing multicasts on a sending socket
357 static void set_mcast_ttl(struct sock *sk, u_char ttl)
359 struct inet_sock *inet = inet_sk(sk);
361 /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
368 * Specifiy default interface for outgoing multicasts
370 static int set_mcast_if(struct sock *sk, char *ifname)
372 struct net_device *dev;
373 struct inet_sock *inet = inet_sk(sk);
375 if ((dev = __dev_get_by_name(ifname)) == NULL)
378 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
382 inet->mc_index = dev->ifindex;
383 /* inet->mc_addr = 0; */
391 * Set the maximum length of sync message according to the
392 * specified interface's MTU.
394 static int set_sync_mesg_maxlen(int sync_state)
396 struct net_device *dev;
399 if (sync_state == IP_VS_STATE_MASTER) {
400 if ((dev = __dev_get_by_name(ip_vs_master_mcast_ifn)) == NULL)
403 num = (dev->mtu - sizeof(struct iphdr) -
404 sizeof(struct udphdr) -
405 SYNC_MESG_HEADER_LEN - 20) / SIMPLE_CONN_SIZE;
406 sync_send_mesg_maxlen =
407 SYNC_MESG_HEADER_LEN + SIMPLE_CONN_SIZE * num;
408 IP_VS_DBG(7, "setting the maximum length of sync sending "
409 "message %d.\n", sync_send_mesg_maxlen);
410 } else if (sync_state == IP_VS_STATE_BACKUP) {
411 if ((dev = __dev_get_by_name(ip_vs_backup_mcast_ifn)) == NULL)
414 sync_recv_mesg_maxlen = dev->mtu -
415 sizeof(struct iphdr) - sizeof(struct udphdr);
416 IP_VS_DBG(7, "setting the maximum length of sync receiving "
417 "message %d.\n", sync_recv_mesg_maxlen);
425 * Join a multicast group.
426 * the group is specified by a class D multicast address 224.0.0.0/8
427 * in the in_addr structure passed in as a parameter.
430 join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
432 struct ip_mreqn mreq;
433 struct net_device *dev;
436 memset(&mreq, 0, sizeof(mreq));
437 memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
439 if ((dev = __dev_get_by_name(ifname)) == NULL)
441 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
444 mreq.imr_ifindex = dev->ifindex;
447 ret = ip_mc_join_group(sk, &mreq);
454 static int bind_mcastif_addr(struct socket *sock, char *ifname)
456 struct net_device *dev;
458 struct sockaddr_in sin;
460 if ((dev = __dev_get_by_name(ifname)) == NULL)
463 addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
465 IP_VS_ERR("You probably need to specify IP address on "
466 "multicast interface.\n");
468 IP_VS_DBG(7, "binding socket with (%s) %u.%u.%u.%u\n",
469 ifname, NIPQUAD(addr));
471 /* Now bind the socket with the address of multicast interface */
472 sin.sin_family = AF_INET;
473 sin.sin_addr.s_addr = addr;
476 return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
480 * Set up sending multicast socket over UDP
482 static struct socket * make_send_sock(void)
486 /* First create a socket */
487 if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
488 IP_VS_ERR("Error during creation of socket; terminating\n");
492 if (set_mcast_if(sock->sk, ip_vs_master_mcast_ifn) < 0) {
493 IP_VS_ERR("Error setting outbound mcast interface\n");
497 set_mcast_loop(sock->sk, 0);
498 set_mcast_ttl(sock->sk, 1);
500 if (bind_mcastif_addr(sock, ip_vs_master_mcast_ifn) < 0) {
501 IP_VS_ERR("Error binding address of the mcast interface\n");
505 if (sock->ops->connect(sock,
506 (struct sockaddr*)&mcast_addr,
507 sizeof(struct sockaddr), 0) < 0) {
508 IP_VS_ERR("Error connecting to the multicast addr\n");
521 * Set up receiving multicast socket over UDP
523 static struct socket * make_receive_sock(void)
527 /* First create a socket */
528 if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
529 IP_VS_ERR("Error during creation of socket; terminating\n");
533 /* it is equivalent to the REUSEADDR option in user-space */
534 sock->sk->sk_reuse = 1;
536 if (sock->ops->bind(sock,
537 (struct sockaddr*)&mcast_addr,
538 sizeof(struct sockaddr)) < 0) {
539 IP_VS_ERR("Error binding to the multicast addr\n");
543 /* join the multicast group */
544 if (join_mcast_group(sock->sk,
545 (struct in_addr*)&mcast_addr.sin_addr,
546 ip_vs_backup_mcast_ifn) < 0) {
547 IP_VS_ERR("Error joining to the multicast group\n");
560 ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
562 struct msghdr msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
567 iov.iov_base = (void *)buffer;
568 iov.iov_len = length;
570 len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
577 ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
583 /* Put size in network byte order */
584 msg->size = htons(msg->size);
586 if (ip_vs_send_async(sock, (char *)msg, msize) != msize)
587 IP_VS_ERR("ip_vs_send_async error\n");
591 ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
593 struct msghdr msg = {NULL,};
599 /* Receive a packet */
600 iov.iov_base = buffer;
601 iov.iov_len = (size_t)buflen;
603 len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, 0);
613 static DECLARE_WAIT_QUEUE_HEAD(sync_wait);
614 static pid_t sync_master_pid = 0;
615 static pid_t sync_backup_pid = 0;
617 static DECLARE_WAIT_QUEUE_HEAD(stop_sync_wait);
618 static int stop_master_sync = 0;
619 static int stop_backup_sync = 0;
621 static void sync_master_loop(void)
624 struct ip_vs_sync_buff *sb;
626 /* create the sending multicast socket */
627 sock = make_send_sock();
631 IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, "
633 ip_vs_master_mcast_ifn, ip_vs_master_syncid);
636 while ((sb=sb_dequeue())) {
637 ip_vs_send_sync_msg(sock, sb->mesg);
638 ip_vs_sync_buff_release(sb);
641 /* check if entries stay in curr_sb for 2 seconds */
642 if ((sb = get_curr_sync_buff(2*HZ))) {
643 ip_vs_send_sync_msg(sock, sb->mesg);
644 ip_vs_sync_buff_release(sb);
647 if (stop_master_sync)
653 /* clean up the sync_buff queue */
654 while ((sb=sb_dequeue())) {
655 ip_vs_sync_buff_release(sb);
658 /* clean up the current sync_buff */
659 if ((sb = get_curr_sync_buff(0))) {
660 ip_vs_sync_buff_release(sb);
663 /* release the sending multicast socket */
668 static void sync_backup_loop(void)
674 if (!(buf = kmalloc(sync_recv_mesg_maxlen, GFP_ATOMIC))) {
675 IP_VS_ERR("sync_backup_loop: kmalloc error\n");
679 /* create the receiving multicast socket */
680 sock = make_receive_sock();
684 IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
686 ip_vs_backup_mcast_ifn, ip_vs_backup_syncid);
689 /* do you have data now? */
690 while (!skb_queue_empty(&(sock->sk->sk_receive_queue))) {
692 ip_vs_receive(sock, buf,
693 sync_recv_mesg_maxlen)) <= 0) {
694 IP_VS_ERR("receiving message error\n");
697 /* disable bottom half, because it accessed the data
698 shared by softirq while getting/creating conns */
700 ip_vs_process_message(buf, len);
704 if (stop_backup_sync)
710 /* release the sending multicast socket */
718 static void set_sync_pid(int sync_state, pid_t sync_pid)
720 if (sync_state == IP_VS_STATE_MASTER)
721 sync_master_pid = sync_pid;
722 else if (sync_state == IP_VS_STATE_BACKUP)
723 sync_backup_pid = sync_pid;
726 static void set_stop_sync(int sync_state, int set)
728 if (sync_state == IP_VS_STATE_MASTER)
729 stop_master_sync = set;
730 else if (sync_state == IP_VS_STATE_BACKUP)
731 stop_backup_sync = set;
733 stop_master_sync = set;
734 stop_backup_sync = set;
738 static int sync_thread(void *startup)
740 DECLARE_WAITQUEUE(wait, current);
745 /* increase the module use count */
746 ip_vs_use_count_inc();
748 if (ip_vs_sync_state & IP_VS_STATE_MASTER && !sync_master_pid) {
749 state = IP_VS_STATE_MASTER;
750 name = "ipvs_syncmaster";
751 } else if (ip_vs_sync_state & IP_VS_STATE_BACKUP && !sync_backup_pid) {
752 state = IP_VS_STATE_BACKUP;
753 name = "ipvs_syncbackup";
756 ip_vs_use_count_dec();
765 /* Block all signals */
766 spin_lock_irq(¤t->sighand->siglock);
767 siginitsetinv(¤t->blocked, 0);
769 spin_unlock_irq(¤t->sighand->siglock);
771 /* set the maximum length of sync message */
772 set_sync_mesg_maxlen(state);
774 /* set up multicast address */
775 mcast_addr.sin_family = AF_INET;
776 mcast_addr.sin_port = htons(IP_VS_SYNC_PORT);
777 mcast_addr.sin_addr.s_addr = htonl(IP_VS_SYNC_GROUP);
779 add_wait_queue(&sync_wait, &wait);
781 set_sync_pid(state, current->pid);
782 complete((struct completion *)startup);
784 /* processing master/backup loop here */
785 if (state == IP_VS_STATE_MASTER)
787 else if (state == IP_VS_STATE_BACKUP)
791 remove_wait_queue(&sync_wait, &wait);
794 set_sync_pid(state, 0);
795 IP_VS_INFO("sync thread stopped!\n");
799 /* decrease the module use count */
800 ip_vs_use_count_dec();
802 set_stop_sync(state, 0);
803 wake_up(&stop_sync_wait);
809 static int fork_sync_thread(void *startup)
813 /* fork the sync thread here, then the parent process of the
814 sync thread is the init process after this thread exits. */
816 if ((pid = kernel_thread(sync_thread, startup, 0)) < 0) {
817 IP_VS_ERR("could not create sync_thread due to %d... "
827 int start_sync_thread(int state, char *mcast_ifn, __u8 syncid)
829 DECLARE_COMPLETION(startup);
832 if ((state == IP_VS_STATE_MASTER && sync_master_pid) ||
833 (state == IP_VS_STATE_BACKUP && sync_backup_pid))
836 IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, current->pid);
837 IP_VS_DBG(7, "Each ip_vs_sync_conn entry need %Zd bytes\n",
838 sizeof(struct ip_vs_sync_conn));
840 ip_vs_sync_state |= state;
841 if (state == IP_VS_STATE_MASTER) {
842 strlcpy(ip_vs_master_mcast_ifn, mcast_ifn, sizeof(ip_vs_master_mcast_ifn));
843 ip_vs_master_syncid = syncid;
845 strlcpy(ip_vs_backup_mcast_ifn, mcast_ifn, sizeof(ip_vs_backup_mcast_ifn));
846 ip_vs_backup_syncid = syncid;
850 if ((pid = kernel_thread(fork_sync_thread, &startup, 0)) < 0) {
851 IP_VS_ERR("could not create fork_sync_thread due to %d... "
857 wait_for_completion(&startup);
863 int stop_sync_thread(int state)
865 DECLARE_WAITQUEUE(wait, current);
867 if ((state == IP_VS_STATE_MASTER && !sync_master_pid) ||
868 (state == IP_VS_STATE_BACKUP && !sync_backup_pid))
871 IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, current->pid);
872 IP_VS_INFO("stopping sync thread %d ...\n",
873 (state == IP_VS_STATE_MASTER) ? sync_master_pid : sync_backup_pid);
875 __set_current_state(TASK_UNINTERRUPTIBLE);
876 add_wait_queue(&stop_sync_wait, &wait);
877 set_stop_sync(state, 1);
878 ip_vs_sync_state -= state;
881 __set_current_state(TASK_RUNNING);
882 remove_wait_queue(&stop_sync_wait, &wait);
884 /* Note: no need to reap the sync thread, because its parent
885 process is the init process */
887 if ((state == IP_VS_STATE_MASTER && stop_master_sync) ||
888 (state == IP_VS_STATE_BACKUP && stop_backup_sync))