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/inetdevice.h>
25 #include <linux/net.h>
26 #include <linux/completion.h>
27 #include <linux/delay.h>
28 #include <linux/skbuff.h>
30 #include <linux/igmp.h> /* for ip_mc_join_group */
31 #include <linux/udp.h>
35 #include <asm/uaccess.h> /* for get_fs and set_fs */
37 #include <net/ip_vs.h>
39 #define IP_VS_SYNC_GROUP 0xe0000051 /* multicast addr - 224.0.0.81 */
40 #define IP_VS_SYNC_PORT 8848 /* multicast port */
44 * IPVS sync connection entry
46 struct ip_vs_sync_conn {
49 /* Protocol, addresses and port numbers */
50 __u8 protocol; /* Which protocol (TCP/UDP) */
54 __be32 caddr; /* client address */
55 __be32 vaddr; /* virtual address */
56 __be32 daddr; /* destination address */
58 /* Flags and state transition */
59 __be16 flags; /* status flags */
60 __be16 state; /* state info */
62 /* The sequence options start here */
65 struct ip_vs_sync_conn_options {
66 struct ip_vs_seq in_seq; /* incoming seq. struct */
67 struct ip_vs_seq out_seq; /* outgoing seq. struct */
70 #define IP_VS_SYNC_CONN_TIMEOUT (3*60*HZ)
71 #define SIMPLE_CONN_SIZE (sizeof(struct ip_vs_sync_conn))
72 #define FULL_CONN_SIZE \
73 (sizeof(struct ip_vs_sync_conn) + sizeof(struct ip_vs_sync_conn_options))
77 The master mulitcasts messages to the backup load balancers in the
81 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
82 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
83 | Count Conns | SyncID | Size |
84 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
86 | IPVS Sync Connection (1) |
87 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93 | IPVS Sync Connection (n) |
94 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97 #define SYNC_MESG_HEADER_LEN 4
99 struct ip_vs_sync_mesg {
104 /* ip_vs_sync_conn entries start here */
107 /* the maximum length of sync (sending/receiving) message */
108 static int sync_send_mesg_maxlen;
109 static int sync_recv_mesg_maxlen;
111 struct ip_vs_sync_buff {
112 struct list_head list;
113 unsigned long firstuse;
115 /* pointers for the message data */
116 struct ip_vs_sync_mesg *mesg;
122 /* the sync_buff list head and the lock */
123 static LIST_HEAD(ip_vs_sync_queue);
124 static DEFINE_SPINLOCK(ip_vs_sync_lock);
126 /* current sync_buff for accepting new conn entries */
127 static struct ip_vs_sync_buff *curr_sb = NULL;
128 static DEFINE_SPINLOCK(curr_sb_lock);
130 /* ipvs sync daemon state */
131 volatile int ip_vs_sync_state = IP_VS_STATE_NONE;
132 volatile int ip_vs_master_syncid = 0;
133 volatile int ip_vs_backup_syncid = 0;
135 /* multicast interface name */
136 char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
137 char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
140 static struct sockaddr_in mcast_addr;
143 static inline void sb_queue_tail(struct ip_vs_sync_buff *sb)
145 spin_lock(&ip_vs_sync_lock);
146 list_add_tail(&sb->list, &ip_vs_sync_queue);
147 spin_unlock(&ip_vs_sync_lock);
150 static inline struct ip_vs_sync_buff * sb_dequeue(void)
152 struct ip_vs_sync_buff *sb;
154 spin_lock_bh(&ip_vs_sync_lock);
155 if (list_empty(&ip_vs_sync_queue)) {
158 sb = list_entry(ip_vs_sync_queue.next,
159 struct ip_vs_sync_buff,
163 spin_unlock_bh(&ip_vs_sync_lock);
168 static inline struct ip_vs_sync_buff * ip_vs_sync_buff_create(void)
170 struct ip_vs_sync_buff *sb;
172 if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
175 if (!(sb->mesg=kmalloc(sync_send_mesg_maxlen, GFP_ATOMIC))) {
179 sb->mesg->nr_conns = 0;
180 sb->mesg->syncid = ip_vs_master_syncid;
182 sb->head = (unsigned char *)sb->mesg + 4;
183 sb->end = (unsigned char *)sb->mesg + sync_send_mesg_maxlen;
184 sb->firstuse = jiffies;
188 static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff *sb)
195 * Get the current sync buffer if it has been created for more
196 * than the specified time or the specified time is zero.
198 static inline struct ip_vs_sync_buff *
199 get_curr_sync_buff(unsigned long time)
201 struct ip_vs_sync_buff *sb;
203 spin_lock_bh(&curr_sb_lock);
204 if (curr_sb && (time == 0 ||
205 time_before(jiffies - curr_sb->firstuse, time))) {
210 spin_unlock_bh(&curr_sb_lock);
216 * Add an ip_vs_conn information into the current sync_buff.
217 * Called by ip_vs_in.
219 void ip_vs_sync_conn(struct ip_vs_conn *cp)
221 struct ip_vs_sync_mesg *m;
222 struct ip_vs_sync_conn *s;
225 spin_lock(&curr_sb_lock);
227 if (!(curr_sb=ip_vs_sync_buff_create())) {
228 spin_unlock(&curr_sb_lock);
229 IP_VS_ERR("ip_vs_sync_buff_create failed.\n");
234 len = (cp->flags & IP_VS_CONN_F_SEQ_MASK) ? FULL_CONN_SIZE :
237 s = (struct ip_vs_sync_conn *)curr_sb->head;
240 s->protocol = cp->protocol;
241 s->cport = cp->cport;
242 s->vport = cp->vport;
243 s->dport = cp->dport;
244 s->caddr = cp->caddr;
245 s->vaddr = cp->vaddr;
246 s->daddr = cp->daddr;
247 s->flags = htons(cp->flags & ~IP_VS_CONN_F_HASHED);
248 s->state = htons(cp->state);
249 if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
250 struct ip_vs_sync_conn_options *opt =
251 (struct ip_vs_sync_conn_options *)&s[1];
252 memcpy(opt, &cp->in_seq, sizeof(*opt));
257 curr_sb->head += len;
259 /* check if there is a space for next one */
260 if (curr_sb->head+FULL_CONN_SIZE > curr_sb->end) {
261 sb_queue_tail(curr_sb);
264 spin_unlock(&curr_sb_lock);
266 /* synchronize its controller if it has */
268 ip_vs_sync_conn(cp->control);
273 * Process received multicast message and create the corresponding
274 * ip_vs_conn entries.
276 static void ip_vs_process_message(const char *buffer, const size_t buflen)
278 struct ip_vs_sync_mesg *m = (struct ip_vs_sync_mesg *)buffer;
279 struct ip_vs_sync_conn *s;
280 struct ip_vs_sync_conn_options *opt;
281 struct ip_vs_conn *cp;
285 /* Convert size back to host byte order */
286 m->size = ntohs(m->size);
288 if (buflen != m->size) {
289 IP_VS_ERR("bogus message\n");
293 /* SyncID sanity check */
294 if (ip_vs_backup_syncid != 0 && m->syncid != ip_vs_backup_syncid) {
295 IP_VS_DBG(7, "Ignoring incoming msg with syncid = %d\n",
300 p = (char *)buffer + sizeof(struct ip_vs_sync_mesg);
301 for (i=0; i<m->nr_conns; i++) {
304 s = (struct ip_vs_sync_conn *)p;
305 flags = ntohs(s->flags);
306 if (!(flags & IP_VS_CONN_F_TEMPLATE))
307 cp = ip_vs_conn_in_get(s->protocol,
311 cp = ip_vs_ct_in_get(s->protocol,
315 cp = ip_vs_conn_new(s->protocol,
321 IP_VS_ERR("ip_vs_conn_new failed\n");
324 cp->state = ntohs(s->state);
325 } else if (!cp->dest) {
326 /* it is an entry created by the synchronization */
327 cp->state = ntohs(s->state);
328 cp->flags = flags | IP_VS_CONN_F_HASHED;
329 } /* Note that we don't touch its state and flags
330 if it is a normal entry. */
332 if (flags & IP_VS_CONN_F_SEQ_MASK) {
333 opt = (struct ip_vs_sync_conn_options *)&s[1];
334 memcpy(&cp->in_seq, opt, sizeof(*opt));
337 p += SIMPLE_CONN_SIZE;
339 atomic_set(&cp->in_pkts, sysctl_ip_vs_sync_threshold[0]);
340 cp->timeout = IP_VS_SYNC_CONN_TIMEOUT;
343 if (p > buffer+buflen) {
344 IP_VS_ERR("bogus message\n");
352 * Setup loopback of outgoing multicasts on a sending socket
354 static void set_mcast_loop(struct sock *sk, u_char loop)
356 struct inet_sock *inet = inet_sk(sk);
358 /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
360 inet->mc_loop = loop ? 1 : 0;
365 * Specify TTL for outgoing multicasts on a sending socket
367 static void set_mcast_ttl(struct sock *sk, u_char ttl)
369 struct inet_sock *inet = inet_sk(sk);
371 /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
378 * Specifiy default interface for outgoing multicasts
380 static int set_mcast_if(struct sock *sk, char *ifname)
382 struct net_device *dev;
383 struct inet_sock *inet = inet_sk(sk);
385 if ((dev = __dev_get_by_name(ifname)) == NULL)
388 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
392 inet->mc_index = dev->ifindex;
393 /* inet->mc_addr = 0; */
401 * Set the maximum length of sync message according to the
402 * specified interface's MTU.
404 static int set_sync_mesg_maxlen(int sync_state)
406 struct net_device *dev;
409 if (sync_state == IP_VS_STATE_MASTER) {
410 if ((dev = __dev_get_by_name(ip_vs_master_mcast_ifn)) == NULL)
413 num = (dev->mtu - sizeof(struct iphdr) -
414 sizeof(struct udphdr) -
415 SYNC_MESG_HEADER_LEN - 20) / SIMPLE_CONN_SIZE;
416 sync_send_mesg_maxlen =
417 SYNC_MESG_HEADER_LEN + SIMPLE_CONN_SIZE * num;
418 IP_VS_DBG(7, "setting the maximum length of sync sending "
419 "message %d.\n", sync_send_mesg_maxlen);
420 } else if (sync_state == IP_VS_STATE_BACKUP) {
421 if ((dev = __dev_get_by_name(ip_vs_backup_mcast_ifn)) == NULL)
424 sync_recv_mesg_maxlen = dev->mtu -
425 sizeof(struct iphdr) - sizeof(struct udphdr);
426 IP_VS_DBG(7, "setting the maximum length of sync receiving "
427 "message %d.\n", sync_recv_mesg_maxlen);
435 * Join a multicast group.
436 * the group is specified by a class D multicast address 224.0.0.0/8
437 * in the in_addr structure passed in as a parameter.
440 join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
442 struct ip_mreqn mreq;
443 struct net_device *dev;
446 memset(&mreq, 0, sizeof(mreq));
447 memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
449 if ((dev = __dev_get_by_name(ifname)) == NULL)
451 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
454 mreq.imr_ifindex = dev->ifindex;
457 ret = ip_mc_join_group(sk, &mreq);
464 static int bind_mcastif_addr(struct socket *sock, char *ifname)
466 struct net_device *dev;
468 struct sockaddr_in sin;
470 if ((dev = __dev_get_by_name(ifname)) == NULL)
473 addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
475 IP_VS_ERR("You probably need to specify IP address on "
476 "multicast interface.\n");
478 IP_VS_DBG(7, "binding socket with (%s) %u.%u.%u.%u\n",
479 ifname, NIPQUAD(addr));
481 /* Now bind the socket with the address of multicast interface */
482 sin.sin_family = AF_INET;
483 sin.sin_addr.s_addr = addr;
486 return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
490 * Set up sending multicast socket over UDP
492 static struct socket * make_send_sock(void)
496 /* First create a socket */
497 if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
498 IP_VS_ERR("Error during creation of socket; terminating\n");
502 if (set_mcast_if(sock->sk, ip_vs_master_mcast_ifn) < 0) {
503 IP_VS_ERR("Error setting outbound mcast interface\n");
507 set_mcast_loop(sock->sk, 0);
508 set_mcast_ttl(sock->sk, 1);
510 if (bind_mcastif_addr(sock, ip_vs_master_mcast_ifn) < 0) {
511 IP_VS_ERR("Error binding address of the mcast interface\n");
515 if (sock->ops->connect(sock,
516 (struct sockaddr*)&mcast_addr,
517 sizeof(struct sockaddr), 0) < 0) {
518 IP_VS_ERR("Error connecting to the multicast addr\n");
531 * Set up receiving multicast socket over UDP
533 static struct socket * make_receive_sock(void)
537 /* First create a socket */
538 if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
539 IP_VS_ERR("Error during creation of socket; terminating\n");
543 /* it is equivalent to the REUSEADDR option in user-space */
544 sock->sk->sk_reuse = 1;
546 if (sock->ops->bind(sock,
547 (struct sockaddr*)&mcast_addr,
548 sizeof(struct sockaddr)) < 0) {
549 IP_VS_ERR("Error binding to the multicast addr\n");
553 /* join the multicast group */
554 if (join_mcast_group(sock->sk,
555 (struct in_addr*)&mcast_addr.sin_addr,
556 ip_vs_backup_mcast_ifn) < 0) {
557 IP_VS_ERR("Error joining to the multicast group\n");
570 ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
572 struct msghdr msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
577 iov.iov_base = (void *)buffer;
578 iov.iov_len = length;
580 len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
587 ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
593 /* Put size in network byte order */
594 msg->size = htons(msg->size);
596 if (ip_vs_send_async(sock, (char *)msg, msize) != msize)
597 IP_VS_ERR("ip_vs_send_async error\n");
601 ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
603 struct msghdr msg = {NULL,};
609 /* Receive a packet */
610 iov.iov_base = buffer;
611 iov.iov_len = (size_t)buflen;
613 len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, 0);
623 static DECLARE_WAIT_QUEUE_HEAD(sync_wait);
624 static pid_t sync_master_pid = 0;
625 static pid_t sync_backup_pid = 0;
627 static DECLARE_WAIT_QUEUE_HEAD(stop_sync_wait);
628 static int stop_master_sync = 0;
629 static int stop_backup_sync = 0;
631 static void sync_master_loop(void)
634 struct ip_vs_sync_buff *sb;
636 /* create the sending multicast socket */
637 sock = make_send_sock();
641 IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, "
643 ip_vs_master_mcast_ifn, ip_vs_master_syncid);
646 while ((sb=sb_dequeue())) {
647 ip_vs_send_sync_msg(sock, sb->mesg);
648 ip_vs_sync_buff_release(sb);
651 /* check if entries stay in curr_sb for 2 seconds */
652 if ((sb = get_curr_sync_buff(2*HZ))) {
653 ip_vs_send_sync_msg(sock, sb->mesg);
654 ip_vs_sync_buff_release(sb);
657 if (stop_master_sync)
660 msleep_interruptible(1000);
663 /* clean up the sync_buff queue */
664 while ((sb=sb_dequeue())) {
665 ip_vs_sync_buff_release(sb);
668 /* clean up the current sync_buff */
669 if ((sb = get_curr_sync_buff(0))) {
670 ip_vs_sync_buff_release(sb);
673 /* release the sending multicast socket */
678 static void sync_backup_loop(void)
684 if (!(buf = kmalloc(sync_recv_mesg_maxlen, GFP_ATOMIC))) {
685 IP_VS_ERR("sync_backup_loop: kmalloc error\n");
689 /* create the receiving multicast socket */
690 sock = make_receive_sock();
694 IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
696 ip_vs_backup_mcast_ifn, ip_vs_backup_syncid);
699 /* do you have data now? */
700 while (!skb_queue_empty(&(sock->sk->sk_receive_queue))) {
702 ip_vs_receive(sock, buf,
703 sync_recv_mesg_maxlen)) <= 0) {
704 IP_VS_ERR("receiving message error\n");
707 /* disable bottom half, because it accessed the data
708 shared by softirq while getting/creating conns */
710 ip_vs_process_message(buf, len);
714 if (stop_backup_sync)
717 msleep_interruptible(1000);
720 /* release the sending multicast socket */
728 static void set_sync_pid(int sync_state, pid_t sync_pid)
730 if (sync_state == IP_VS_STATE_MASTER)
731 sync_master_pid = sync_pid;
732 else if (sync_state == IP_VS_STATE_BACKUP)
733 sync_backup_pid = sync_pid;
736 static void set_stop_sync(int sync_state, int set)
738 if (sync_state == IP_VS_STATE_MASTER)
739 stop_master_sync = set;
740 else if (sync_state == IP_VS_STATE_BACKUP)
741 stop_backup_sync = set;
743 stop_master_sync = set;
744 stop_backup_sync = set;
748 static int sync_thread(void *startup)
750 DECLARE_WAITQUEUE(wait, current);
755 /* increase the module use count */
756 ip_vs_use_count_inc();
758 if (ip_vs_sync_state & IP_VS_STATE_MASTER && !sync_master_pid) {
759 state = IP_VS_STATE_MASTER;
760 name = "ipvs_syncmaster";
761 } else if (ip_vs_sync_state & IP_VS_STATE_BACKUP && !sync_backup_pid) {
762 state = IP_VS_STATE_BACKUP;
763 name = "ipvs_syncbackup";
766 ip_vs_use_count_dec();
775 /* Block all signals */
776 spin_lock_irq(¤t->sighand->siglock);
777 siginitsetinv(¤t->blocked, 0);
779 spin_unlock_irq(¤t->sighand->siglock);
781 /* set the maximum length of sync message */
782 set_sync_mesg_maxlen(state);
784 /* set up multicast address */
785 mcast_addr.sin_family = AF_INET;
786 mcast_addr.sin_port = htons(IP_VS_SYNC_PORT);
787 mcast_addr.sin_addr.s_addr = htonl(IP_VS_SYNC_GROUP);
789 add_wait_queue(&sync_wait, &wait);
791 set_sync_pid(state, current->pid);
792 complete((struct completion *)startup);
794 /* processing master/backup loop here */
795 if (state == IP_VS_STATE_MASTER)
797 else if (state == IP_VS_STATE_BACKUP)
801 remove_wait_queue(&sync_wait, &wait);
804 set_sync_pid(state, 0);
805 IP_VS_INFO("sync thread stopped!\n");
809 /* decrease the module use count */
810 ip_vs_use_count_dec();
812 set_stop_sync(state, 0);
813 wake_up(&stop_sync_wait);
819 static int fork_sync_thread(void *startup)
823 /* fork the sync thread here, then the parent process of the
824 sync thread is the init process after this thread exits. */
826 if ((pid = kernel_thread(sync_thread, startup, 0)) < 0) {
827 IP_VS_ERR("could not create sync_thread due to %d... "
829 msleep_interruptible(1000);
837 int start_sync_thread(int state, char *mcast_ifn, __u8 syncid)
839 DECLARE_COMPLETION_ONSTACK(startup);
842 if ((state == IP_VS_STATE_MASTER && sync_master_pid) ||
843 (state == IP_VS_STATE_BACKUP && sync_backup_pid))
846 IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, current->pid);
847 IP_VS_DBG(7, "Each ip_vs_sync_conn entry need %Zd bytes\n",
848 sizeof(struct ip_vs_sync_conn));
850 ip_vs_sync_state |= state;
851 if (state == IP_VS_STATE_MASTER) {
852 strlcpy(ip_vs_master_mcast_ifn, mcast_ifn,
853 sizeof(ip_vs_master_mcast_ifn));
854 ip_vs_master_syncid = syncid;
856 strlcpy(ip_vs_backup_mcast_ifn, mcast_ifn,
857 sizeof(ip_vs_backup_mcast_ifn));
858 ip_vs_backup_syncid = syncid;
862 if ((pid = kernel_thread(fork_sync_thread, &startup, 0)) < 0) {
863 IP_VS_ERR("could not create fork_sync_thread due to %d... "
865 msleep_interruptible(1000);
869 wait_for_completion(&startup);
875 int stop_sync_thread(int state)
877 DECLARE_WAITQUEUE(wait, current);
879 if ((state == IP_VS_STATE_MASTER && !sync_master_pid) ||
880 (state == IP_VS_STATE_BACKUP && !sync_backup_pid))
883 IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, current->pid);
884 IP_VS_INFO("stopping sync thread %d ...\n",
885 (state == IP_VS_STATE_MASTER) ?
886 sync_master_pid : sync_backup_pid);
888 __set_current_state(TASK_UNINTERRUPTIBLE);
889 add_wait_queue(&stop_sync_wait, &wait);
890 set_stop_sync(state, 1);
891 ip_vs_sync_state -= state;
894 __set_current_state(TASK_RUNNING);
895 remove_wait_queue(&stop_sync_wait, &wait);
897 /* Note: no need to reap the sync thread, because its parent
898 process is the init process */
900 if ((state == IP_VS_STATE_MASTER && stop_master_sync) ||
901 (state == IP_VS_STATE_BACKUP && stop_backup_sync))