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
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/net.h>
24 #include <linux/completion.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 | SyncID | Size |
81 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
83 | IPVS Sync Connection (1) |
84 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
88 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90 | IPVS Sync Connection (n) |
91 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 #define SYNC_MESG_HEADER_LEN 4
96 struct ip_vs_sync_mesg {
101 /* ip_vs_sync_conn entries start here */
104 /* the maximum length of sync (sending/receiving) message */
105 static int sync_send_mesg_maxlen;
106 static int sync_recv_mesg_maxlen;
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_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
134 char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
137 static struct sockaddr_in mcast_addr;
140 static inline void sb_queue_tail(struct ip_vs_sync_buff *sb)
142 spin_lock(&ip_vs_sync_lock);
143 list_add_tail(&sb->list, &ip_vs_sync_queue);
144 spin_unlock(&ip_vs_sync_lock);
147 static inline struct ip_vs_sync_buff * sb_dequeue(void)
149 struct ip_vs_sync_buff *sb;
151 spin_lock_bh(&ip_vs_sync_lock);
152 if (list_empty(&ip_vs_sync_queue)) {
155 sb = list_entry(ip_vs_sync_queue.next,
156 struct ip_vs_sync_buff,
160 spin_unlock_bh(&ip_vs_sync_lock);
165 static inline struct ip_vs_sync_buff * ip_vs_sync_buff_create(void)
167 struct ip_vs_sync_buff *sb;
169 if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
172 if (!(sb->mesg=kmalloc(sync_send_mesg_maxlen, GFP_ATOMIC))) {
176 sb->mesg->nr_conns = 0;
177 sb->mesg->syncid = ip_vs_master_syncid;
179 sb->head = (unsigned char *)sb->mesg + 4;
180 sb->end = (unsigned char *)sb->mesg + sync_send_mesg_maxlen;
181 sb->firstuse = jiffies;
185 static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff *sb)
192 * Get the current sync buffer if it has been created for more
193 * than the specified time or the specified time is zero.
195 static inline struct ip_vs_sync_buff *
196 get_curr_sync_buff(unsigned long time)
198 struct ip_vs_sync_buff *sb;
200 spin_lock_bh(&curr_sb_lock);
201 if (curr_sb && (time == 0 ||
202 time_before(jiffies - curr_sb->firstuse, time))) {
207 spin_unlock_bh(&curr_sb_lock);
213 * Add an ip_vs_conn information into the current sync_buff.
214 * Called by ip_vs_in.
216 void ip_vs_sync_conn(struct ip_vs_conn *cp)
218 struct ip_vs_sync_mesg *m;
219 struct ip_vs_sync_conn *s;
222 spin_lock(&curr_sb_lock);
224 if (!(curr_sb=ip_vs_sync_buff_create())) {
225 spin_unlock(&curr_sb_lock);
226 IP_VS_ERR("ip_vs_sync_buff_create failed.\n");
231 len = (cp->flags & IP_VS_CONN_F_SEQ_MASK) ? FULL_CONN_SIZE :
234 s = (struct ip_vs_sync_conn *)curr_sb->head;
237 s->protocol = cp->protocol;
238 s->cport = cp->cport;
239 s->vport = cp->vport;
240 s->dport = cp->dport;
241 s->caddr = cp->caddr;
242 s->vaddr = cp->vaddr;
243 s->daddr = cp->daddr;
244 s->flags = htons(cp->flags & ~IP_VS_CONN_F_HASHED);
245 s->state = htons(cp->state);
246 if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
247 struct ip_vs_sync_conn_options *opt =
248 (struct ip_vs_sync_conn_options *)&s[1];
249 memcpy(opt, &cp->in_seq, sizeof(*opt));
254 curr_sb->head += len;
256 /* check if there is a space for next one */
257 if (curr_sb->head+FULL_CONN_SIZE > curr_sb->end) {
258 sb_queue_tail(curr_sb);
261 spin_unlock(&curr_sb_lock);
263 /* synchronize its controller if it has */
265 ip_vs_sync_conn(cp->control);
270 * Process received multicast message and create the corresponding
271 * ip_vs_conn entries.
273 static void ip_vs_process_message(const char *buffer, const size_t buflen)
275 struct ip_vs_sync_mesg *m = (struct ip_vs_sync_mesg *)buffer;
276 struct ip_vs_sync_conn *s;
277 struct ip_vs_sync_conn_options *opt;
278 struct ip_vs_conn *cp;
282 if (buflen != m->size) {
283 IP_VS_ERR("bogus message\n");
287 /* SyncID sanity check */
288 if (ip_vs_backup_syncid != 0 && m->syncid != ip_vs_backup_syncid) {
289 IP_VS_DBG(7, "Ignoring incoming msg with syncid = %d\n",
294 p = (char *)buffer + sizeof(struct ip_vs_sync_mesg);
295 for (i=0; i<m->nr_conns; i++) {
296 s = (struct ip_vs_sync_conn *)p;
297 cp = ip_vs_conn_in_get(s->protocol,
301 cp = ip_vs_conn_new(s->protocol,
305 ntohs(s->flags), NULL);
307 IP_VS_ERR("ip_vs_conn_new failed\n");
310 cp->state = ntohs(s->state);
311 } else if (!cp->dest) {
312 /* it is an entry created by the synchronization */
313 cp->state = ntohs(s->state);
314 cp->flags = ntohs(s->flags) | IP_VS_CONN_F_HASHED;
315 } /* Note that we don't touch its state and flags
316 if it is a normal entry. */
318 if (ntohs(s->flags) & IP_VS_CONN_F_SEQ_MASK) {
319 opt = (struct ip_vs_sync_conn_options *)&s[1];
320 memcpy(&cp->in_seq, opt, sizeof(*opt));
323 p += SIMPLE_CONN_SIZE;
325 atomic_set(&cp->in_pkts, sysctl_ip_vs_sync_threshold[0]);
326 cp->timeout = IP_VS_SYNC_CONN_TIMEOUT;
329 if (p > buffer+buflen) {
330 IP_VS_ERR("bogus message\n");
338 * Setup loopback of outgoing multicasts on a sending socket
340 static void set_mcast_loop(struct sock *sk, u_char loop)
342 struct inet_opt *inet = inet_sk(sk);
344 /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
346 inet->mc_loop = loop ? 1 : 0;
351 * Specify TTL for outgoing multicasts on a sending socket
353 static void set_mcast_ttl(struct sock *sk, u_char ttl)
355 struct inet_opt *inet = inet_sk(sk);
357 /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
364 * Specifiy default interface for outgoing multicasts
366 static int set_mcast_if(struct sock *sk, char *ifname)
368 struct net_device *dev;
369 struct inet_opt *inet = inet_sk(sk);
371 if ((dev = __dev_get_by_name(ifname)) == NULL)
374 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
378 inet->mc_index = dev->ifindex;
379 /* inet->mc_addr = 0; */
387 * Set the maximum length of sync message according to the
388 * specified interface's MTU.
390 static int set_sync_mesg_maxlen(int sync_state)
392 struct net_device *dev;
395 if (sync_state == IP_VS_STATE_MASTER) {
396 if ((dev = __dev_get_by_name(ip_vs_master_mcast_ifn)) == NULL)
399 num = (dev->mtu - sizeof(struct iphdr) -
400 sizeof(struct udphdr) -
401 SYNC_MESG_HEADER_LEN - 20) / SIMPLE_CONN_SIZE;
402 sync_send_mesg_maxlen =
403 SYNC_MESG_HEADER_LEN + SIMPLE_CONN_SIZE * num;
404 IP_VS_DBG(7, "setting the maximum length of sync sending "
405 "message %d.\n", sync_send_mesg_maxlen);
406 } else if (sync_state == IP_VS_STATE_BACKUP) {
407 if ((dev = __dev_get_by_name(ip_vs_backup_mcast_ifn)) == NULL)
410 sync_recv_mesg_maxlen = dev->mtu -
411 sizeof(struct iphdr) - sizeof(struct udphdr);
412 IP_VS_DBG(7, "setting the maximum length of sync receiving "
413 "message %d.\n", sync_recv_mesg_maxlen);
421 * Join a multicast group.
422 * the group is specified by a class D multicast address 224.0.0.0/8
423 * in the in_addr structure passed in as a parameter.
426 join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
428 struct ip_mreqn mreq;
429 struct net_device *dev;
432 memset(&mreq, 0, sizeof(mreq));
433 memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
435 if ((dev = __dev_get_by_name(ifname)) == NULL)
437 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
440 mreq.imr_ifindex = dev->ifindex;
443 ret = ip_mc_join_group(sk, &mreq);
450 static int bind_mcastif_addr(struct socket *sock, char *ifname)
452 struct net_device *dev;
454 struct sockaddr_in sin;
456 if ((dev = __dev_get_by_name(ifname)) == NULL)
459 addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
461 IP_VS_ERR("You probably need to specify IP address on "
462 "multicast interface.\n");
464 IP_VS_DBG(7, "binding socket with (%s) %u.%u.%u.%u\n",
465 ifname, NIPQUAD(addr));
467 /* Now bind the socket with the address of multicast interface */
468 sin.sin_family = AF_INET;
469 sin.sin_addr.s_addr = addr;
472 return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
476 * Set up sending multicast socket over UDP
478 static struct socket * make_send_sock(void)
482 /* First create a socket */
483 if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
484 IP_VS_ERR("Error during creation of socket; terminating\n");
488 if (set_mcast_if(sock->sk, ip_vs_master_mcast_ifn) < 0) {
489 IP_VS_ERR("Error setting outbound mcast interface\n");
493 set_mcast_loop(sock->sk, 0);
494 set_mcast_ttl(sock->sk, 1);
496 if (bind_mcastif_addr(sock, ip_vs_master_mcast_ifn) < 0) {
497 IP_VS_ERR("Error binding address of the mcast interface\n");
501 if (sock->ops->connect(sock,
502 (struct sockaddr*)&mcast_addr,
503 sizeof(struct sockaddr), 0) < 0) {
504 IP_VS_ERR("Error connecting to the multicast addr\n");
517 * Set up receiving multicast socket over UDP
519 static struct socket * make_receive_sock(void)
523 /* First create a socket */
524 if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
525 IP_VS_ERR("Error during creation of socket; terminating\n");
529 /* it is equivalent to the REUSEADDR option in user-space */
530 sock->sk->sk_reuse = 1;
532 if (sock->ops->bind(sock,
533 (struct sockaddr*)&mcast_addr,
534 sizeof(struct sockaddr)) < 0) {
535 IP_VS_ERR("Error binding to the multicast addr\n");
539 /* join the multicast group */
540 if (join_mcast_group(sock->sk,
541 (struct in_addr*)&mcast_addr.sin_addr,
542 ip_vs_backup_mcast_ifn) < 0) {
543 IP_VS_ERR("Error joining to the multicast group\n");
556 ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
558 struct msghdr msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
563 iov.iov_base = (void *)buffer;
564 iov.iov_len = length;
566 len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
574 ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
576 struct msghdr msg = {NULL,};
582 /* Receive a packet */
583 iov.iov_base = buffer;
584 iov.iov_len = (size_t)buflen;
586 len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, 0);
596 static DECLARE_WAIT_QUEUE_HEAD(sync_wait);
597 static pid_t sync_master_pid = 0;
598 static pid_t sync_backup_pid = 0;
600 static DECLARE_WAIT_QUEUE_HEAD(stop_sync_wait);
601 static int stop_master_sync = 0;
602 static int stop_backup_sync = 0;
604 static void sync_master_loop(void)
607 struct ip_vs_sync_buff *sb;
608 struct ip_vs_sync_mesg *m;
610 /* create the sending multicast socket */
611 sock = make_send_sock();
615 IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, "
617 ip_vs_master_mcast_ifn, ip_vs_master_syncid);
620 while ((sb=sb_dequeue())) {
622 if (ip_vs_send_async(sock, (char *)m,
624 IP_VS_ERR("ip_vs_send_async error\n");
625 ip_vs_sync_buff_release(sb);
628 /* check if entries stay in curr_sb for 2 seconds */
629 if ((sb = get_curr_sync_buff(2*HZ))) {
631 if (ip_vs_send_async(sock, (char *)m,
633 IP_VS_ERR("ip_vs_send_async error\n");
634 ip_vs_sync_buff_release(sb);
637 if (stop_master_sync)
640 __set_current_state(TASK_INTERRUPTIBLE);
641 schedule_timeout(HZ);
644 /* clean up the sync_buff queue */
645 while ((sb=sb_dequeue())) {
646 ip_vs_sync_buff_release(sb);
649 /* clean up the current sync_buff */
650 if ((sb = get_curr_sync_buff(0))) {
651 ip_vs_sync_buff_release(sb);
654 /* release the sending multicast socket */
659 static void sync_backup_loop(void)
665 if (!(buf = kmalloc(sync_recv_mesg_maxlen, GFP_ATOMIC))) {
666 IP_VS_ERR("sync_backup_loop: kmalloc error\n");
670 /* create the receiving multicast socket */
671 sock = make_receive_sock();
675 IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
677 ip_vs_backup_mcast_ifn, ip_vs_backup_syncid);
680 /* do you have data now? */
681 while (!skb_queue_empty(&(sock->sk->sk_receive_queue))) {
683 ip_vs_receive(sock, buf,
684 sync_recv_mesg_maxlen)) <= 0) {
685 IP_VS_ERR("receiving message error\n");
688 /* disable bottom half, because it accessed the data
689 shared by softirq while getting/creating conns */
691 ip_vs_process_message(buf, len);
695 if (stop_backup_sync)
698 __set_current_state(TASK_INTERRUPTIBLE);
699 schedule_timeout(HZ);
702 /* release the sending multicast socket */
710 static void set_sync_pid(int sync_state, pid_t sync_pid)
712 if (sync_state == IP_VS_STATE_MASTER)
713 sync_master_pid = sync_pid;
714 else if (sync_state == IP_VS_STATE_BACKUP)
715 sync_backup_pid = sync_pid;
718 static void set_stop_sync(int sync_state, int set)
720 if (sync_state == IP_VS_STATE_MASTER)
721 stop_master_sync = set;
722 else if (sync_state == IP_VS_STATE_BACKUP)
723 stop_backup_sync = set;
725 stop_master_sync = set;
726 stop_backup_sync = set;
730 static int sync_thread(void *startup)
732 DECLARE_WAITQUEUE(wait, current);
737 /* increase the module use count */
738 ip_vs_use_count_inc();
740 if (ip_vs_sync_state & IP_VS_STATE_MASTER && !sync_master_pid) {
741 state = IP_VS_STATE_MASTER;
742 name = "ipvs_syncmaster";
743 } else if (ip_vs_sync_state & IP_VS_STATE_BACKUP && !sync_backup_pid) {
744 state = IP_VS_STATE_BACKUP;
745 name = "ipvs_syncbackup";
748 ip_vs_use_count_dec();
757 /* Block all signals */
758 spin_lock_irq(¤t->sighand->siglock);
759 siginitsetinv(¤t->blocked, 0);
761 spin_unlock_irq(¤t->sighand->siglock);
763 /* set the maximum length of sync message */
764 set_sync_mesg_maxlen(state);
766 /* set up multicast address */
767 mcast_addr.sin_family = AF_INET;
768 mcast_addr.sin_port = htons(IP_VS_SYNC_PORT);
769 mcast_addr.sin_addr.s_addr = htonl(IP_VS_SYNC_GROUP);
771 add_wait_queue(&sync_wait, &wait);
773 set_sync_pid(state, current->pid);
774 complete((struct completion *)startup);
776 /* processing master/backup loop here */
777 if (state == IP_VS_STATE_MASTER)
779 else if (state == IP_VS_STATE_BACKUP)
783 remove_wait_queue(&sync_wait, &wait);
786 set_sync_pid(state, 0);
787 IP_VS_INFO("sync thread stopped!\n");
791 /* decrease the module use count */
792 ip_vs_use_count_dec();
794 set_stop_sync(state, 0);
795 wake_up(&stop_sync_wait);
801 static int fork_sync_thread(void *startup)
805 /* fork the sync thread here, then the parent process of the
806 sync thread is the init process after this thread exits. */
808 if ((pid = kernel_thread(sync_thread, startup, 0)) < 0) {
809 IP_VS_ERR("could not create sync_thread due to %d... "
811 current->state = TASK_UNINTERRUPTIBLE;
812 schedule_timeout(HZ);
820 int start_sync_thread(int state, char *mcast_ifn, __u8 syncid)
822 DECLARE_COMPLETION(startup);
825 if ((state == IP_VS_STATE_MASTER && sync_master_pid) ||
826 (state == IP_VS_STATE_BACKUP && sync_backup_pid))
829 IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, current->pid);
830 IP_VS_DBG(7, "Each ip_vs_sync_conn entry need %Zd bytes\n",
831 sizeof(struct ip_vs_sync_conn));
833 ip_vs_sync_state |= state;
834 if (state == IP_VS_STATE_MASTER) {
835 strcpy(ip_vs_master_mcast_ifn, mcast_ifn);
836 ip_vs_master_syncid = syncid;
838 strcpy(ip_vs_backup_mcast_ifn, mcast_ifn);
839 ip_vs_backup_syncid = syncid;
843 if ((pid = kernel_thread(fork_sync_thread, &startup, 0)) < 0) {
844 IP_VS_ERR("could not create fork_sync_thread due to %d... "
846 current->state = TASK_UNINTERRUPTIBLE;
847 schedule_timeout(HZ);
851 wait_for_completion(&startup);
857 int stop_sync_thread(int state)
859 DECLARE_WAITQUEUE(wait, current);
861 if ((state == IP_VS_STATE_MASTER && !sync_master_pid) ||
862 (state == IP_VS_STATE_BACKUP && !sync_backup_pid))
865 IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, current->pid);
866 IP_VS_INFO("stopping sync thread %d ...\n",
867 (state == IP_VS_STATE_MASTER) ? sync_master_pid : sync_backup_pid);
869 __set_current_state(TASK_UNINTERRUPTIBLE);
870 add_wait_queue(&stop_sync_wait, &wait);
871 set_stop_sync(state, 1);
872 ip_vs_sync_state -= state;
875 __set_current_state(TASK_RUNNING);
876 remove_wait_queue(&stop_sync_wait, &wait);
878 /* Note: no need to reap the sync thread, because its parent
879 process is the init process */
881 if ((state == IP_VS_STATE_MASTER && stop_master_sync) ||
882 (state == IP_VS_STATE_BACKUP && stop_backup_sync))