blob: 2e5ced3d80622b77577826da486e721da6bed560 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
6 * cluster of servers.
7 *
8 * Version: $Id: ip_vs_sync.c,v 1.13 2003/06/08 09:31:19 wensong Exp $
9 *
10 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
11 *
12 * ip_vs_sync: sync connection info from master load balancer to backups
13 * through multicast
14 *
15 * Changes:
16 * Alexandre Cassen : Added master & backup support at a time.
17 * Alexandre Cassen : Added SyncID support for incoming sync
18 * messages filtering.
19 * Justin Ossevoort : Fix endian problem on sync message size.
20 */
21
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>
28#include <linux/in.h>
29#include <linux/igmp.h> /* for ip_mc_join_group */
30
31#include <net/ip.h>
32#include <net/sock.h>
33#include <asm/uaccess.h> /* for get_fs and set_fs */
34
35#include <net/ip_vs.h>
36
37#define IP_VS_SYNC_GROUP 0xe0000051 /* multicast addr - 224.0.0.81 */
38#define IP_VS_SYNC_PORT 8848 /* multicast port */
39
40
41/*
42 * IPVS sync connection entry
43 */
44struct ip_vs_sync_conn {
45 __u8 reserved;
46
47 /* Protocol, addresses and port numbers */
48 __u8 protocol; /* Which protocol (TCP/UDP) */
49 __u16 cport;
50 __u16 vport;
51 __u16 dport;
52 __u32 caddr; /* client address */
53 __u32 vaddr; /* virtual address */
54 __u32 daddr; /* destination address */
55
56 /* Flags and state transition */
57 __u16 flags; /* status flags */
58 __u16 state; /* state info */
59
60 /* The sequence options start here */
61};
62
63struct 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 */
66};
67
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))
72
73
74/*
75 The master mulitcasts messages to the backup load balancers in the
76 following format.
77
78 0 1 2 3
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
83 | |
84 | IPVS Sync Connection (1) |
85 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
86 | . |
87 | . |
88 | . |
89 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90 | |
91 | IPVS Sync Connection (n) |
92 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93*/
94
95#define SYNC_MESG_HEADER_LEN 4
96
97struct ip_vs_sync_mesg {
98 __u8 nr_conns;
99 __u8 syncid;
100 __u16 size;
101
102 /* ip_vs_sync_conn entries start here */
103};
104
105/* the maximum length of sync (sending/receiving) message */
106static int sync_send_mesg_maxlen;
107static int sync_recv_mesg_maxlen;
108
109struct ip_vs_sync_buff {
110 struct list_head list;
111 unsigned long firstuse;
112
113 /* pointers for the message data */
114 struct ip_vs_sync_mesg *mesg;
115 unsigned char *head;
116 unsigned char *end;
117};
118
119
120/* the sync_buff list head and the lock */
121static LIST_HEAD(ip_vs_sync_queue);
122static DEFINE_SPINLOCK(ip_vs_sync_lock);
123
124/* current sync_buff for accepting new conn entries */
125static struct ip_vs_sync_buff *curr_sb = NULL;
126static DEFINE_SPINLOCK(curr_sb_lock);
127
128/* ipvs sync daemon state */
129volatile int ip_vs_sync_state = IP_VS_STATE_NONE;
130volatile int ip_vs_master_syncid = 0;
131volatile int ip_vs_backup_syncid = 0;
132
133/* multicast interface name */
134char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
135char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
136
137/* multicast addr */
138static struct sockaddr_in mcast_addr;
139
140
141static inline void sb_queue_tail(struct ip_vs_sync_buff *sb)
142{
143 spin_lock(&ip_vs_sync_lock);
144 list_add_tail(&sb->list, &ip_vs_sync_queue);
145 spin_unlock(&ip_vs_sync_lock);
146}
147
148static inline struct ip_vs_sync_buff * sb_dequeue(void)
149{
150 struct ip_vs_sync_buff *sb;
151
152 spin_lock_bh(&ip_vs_sync_lock);
153 if (list_empty(&ip_vs_sync_queue)) {
154 sb = NULL;
155 } else {
156 sb = list_entry(ip_vs_sync_queue.next,
157 struct ip_vs_sync_buff,
158 list);
159 list_del(&sb->list);
160 }
161 spin_unlock_bh(&ip_vs_sync_lock);
162
163 return sb;
164}
165
166static inline struct ip_vs_sync_buff * ip_vs_sync_buff_create(void)
167{
168 struct ip_vs_sync_buff *sb;
169
170 if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
171 return NULL;
172
173 if (!(sb->mesg=kmalloc(sync_send_mesg_maxlen, GFP_ATOMIC))) {
174 kfree(sb);
175 return NULL;
176 }
177 sb->mesg->nr_conns = 0;
178 sb->mesg->syncid = ip_vs_master_syncid;
179 sb->mesg->size = 4;
180 sb->head = (unsigned char *)sb->mesg + 4;
181 sb->end = (unsigned char *)sb->mesg + sync_send_mesg_maxlen;
182 sb->firstuse = jiffies;
183 return sb;
184}
185
186static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff *sb)
187{
188 kfree(sb->mesg);
189 kfree(sb);
190}
191
192/*
193 * Get the current sync buffer if it has been created for more
194 * than the specified time or the specified time is zero.
195 */
196static inline struct ip_vs_sync_buff *
197get_curr_sync_buff(unsigned long time)
198{
199 struct ip_vs_sync_buff *sb;
200
201 spin_lock_bh(&curr_sb_lock);
202 if (curr_sb && (time == 0 ||
203 time_before(jiffies - curr_sb->firstuse, time))) {
204 sb = curr_sb;
205 curr_sb = NULL;
206 } else
207 sb = NULL;
208 spin_unlock_bh(&curr_sb_lock);
209 return sb;
210}
211
212
213/*
214 * Add an ip_vs_conn information into the current sync_buff.
215 * Called by ip_vs_in.
216 */
217void ip_vs_sync_conn(struct ip_vs_conn *cp)
218{
219 struct ip_vs_sync_mesg *m;
220 struct ip_vs_sync_conn *s;
221 int len;
222
223 spin_lock(&curr_sb_lock);
224 if (!curr_sb) {
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");
228 return;
229 }
230 }
231
232 len = (cp->flags & IP_VS_CONN_F_SEQ_MASK) ? FULL_CONN_SIZE :
233 SIMPLE_CONN_SIZE;
234 m = curr_sb->mesg;
235 s = (struct ip_vs_sync_conn *)curr_sb->head;
236
237 /* copy members */
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));
251 }
252
253 m->nr_conns++;
254 m->size += len;
255 curr_sb->head += len;
256
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);
260 curr_sb = NULL;
261 }
262 spin_unlock(&curr_sb_lock);
263
264 /* synchronize its controller if it has */
265 if (cp->control)
266 ip_vs_sync_conn(cp->control);
267}
268
269
270/*
271 * Process received multicast message and create the corresponding
272 * ip_vs_conn entries.
273 */
274static void ip_vs_process_message(const char *buffer, const size_t buflen)
275{
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;
280 char *p;
281 int i;
282
283 /* Convert size back to host byte order */
284 m->size = ntohs(m->size);
285
286 if (buflen != m->size) {
287 IP_VS_ERR("bogus message\n");
288 return;
289 }
290
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",
294 m->syncid);
295 return;
296 }
297
298 p = (char *)buffer + sizeof(struct ip_vs_sync_mesg);
299 for (i=0; i<m->nr_conns; i++) {
Julian Anastasov87375ab2005-09-14 21:08:51 -0700300 unsigned flags;
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 s = (struct ip_vs_sync_conn *)p;
Julian Anastasov87375ab2005-09-14 21:08:51 -0700303 flags = ntohs(s->flags);
304 if (!(flags & IP_VS_CONN_F_TEMPLATE))
305 cp = ip_vs_conn_in_get(s->protocol,
306 s->caddr, s->cport,
307 s->vaddr, s->vport);
308 else
309 cp = ip_vs_ct_in_get(s->protocol,
310 s->caddr, s->cport,
311 s->vaddr, s->vport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (!cp) {
313 cp = ip_vs_conn_new(s->protocol,
314 s->caddr, s->cport,
315 s->vaddr, s->vport,
316 s->daddr, s->dport,
Julian Anastasov87375ab2005-09-14 21:08:51 -0700317 flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (!cp) {
319 IP_VS_ERR("ip_vs_conn_new failed\n");
320 return;
321 }
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);
Julian Anastasov87375ab2005-09-14 21:08:51 -0700326 cp->flags = flags | IP_VS_CONN_F_HASHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 } /* Note that we don't touch its state and flags
328 if it is a normal entry. */
329
Julian Anastasov87375ab2005-09-14 21:08:51 -0700330 if (flags & IP_VS_CONN_F_SEQ_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 opt = (struct ip_vs_sync_conn_options *)&s[1];
332 memcpy(&cp->in_seq, opt, sizeof(*opt));
333 p += FULL_CONN_SIZE;
334 } else
335 p += SIMPLE_CONN_SIZE;
336
337 atomic_set(&cp->in_pkts, sysctl_ip_vs_sync_threshold[0]);
338 cp->timeout = IP_VS_SYNC_CONN_TIMEOUT;
339 ip_vs_conn_put(cp);
340
341 if (p > buffer+buflen) {
342 IP_VS_ERR("bogus message\n");
343 return;
344 }
345 }
346}
347
348
349/*
350 * Setup loopback of outgoing multicasts on a sending socket
351 */
352static void set_mcast_loop(struct sock *sk, u_char loop)
353{
354 struct inet_sock *inet = inet_sk(sk);
355
356 /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
357 lock_sock(sk);
358 inet->mc_loop = loop ? 1 : 0;
359 release_sock(sk);
360}
361
362/*
363 * Specify TTL for outgoing multicasts on a sending socket
364 */
365static void set_mcast_ttl(struct sock *sk, u_char ttl)
366{
367 struct inet_sock *inet = inet_sk(sk);
368
369 /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
370 lock_sock(sk);
371 inet->mc_ttl = ttl;
372 release_sock(sk);
373}
374
375/*
376 * Specifiy default interface for outgoing multicasts
377 */
378static int set_mcast_if(struct sock *sk, char *ifname)
379{
380 struct net_device *dev;
381 struct inet_sock *inet = inet_sk(sk);
382
383 if ((dev = __dev_get_by_name(ifname)) == NULL)
384 return -ENODEV;
385
386 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
387 return -EINVAL;
388
389 lock_sock(sk);
390 inet->mc_index = dev->ifindex;
391 /* inet->mc_addr = 0; */
392 release_sock(sk);
393
394 return 0;
395}
396
397
398/*
399 * Set the maximum length of sync message according to the
400 * specified interface's MTU.
401 */
402static int set_sync_mesg_maxlen(int sync_state)
403{
404 struct net_device *dev;
405 int num;
406
407 if (sync_state == IP_VS_STATE_MASTER) {
408 if ((dev = __dev_get_by_name(ip_vs_master_mcast_ifn)) == NULL)
409 return -ENODEV;
410
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)
420 return -ENODEV;
421
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);
426 }
427
428 return 0;
429}
430
431
432/*
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.
436 */
437static int
438join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
439{
440 struct ip_mreqn mreq;
441 struct net_device *dev;
442 int ret;
443
444 memset(&mreq, 0, sizeof(mreq));
445 memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
446
447 if ((dev = __dev_get_by_name(ifname)) == NULL)
448 return -ENODEV;
449 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
450 return -EINVAL;
451
452 mreq.imr_ifindex = dev->ifindex;
453
454 lock_sock(sk);
455 ret = ip_mc_join_group(sk, &mreq);
456 release_sock(sk);
457
458 return ret;
459}
460
461
462static int bind_mcastif_addr(struct socket *sock, char *ifname)
463{
464 struct net_device *dev;
465 u32 addr;
466 struct sockaddr_in sin;
467
468 if ((dev = __dev_get_by_name(ifname)) == NULL)
469 return -ENODEV;
470
471 addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
472 if (!addr)
473 IP_VS_ERR("You probably need to specify IP address on "
474 "multicast interface.\n");
475
476 IP_VS_DBG(7, "binding socket with (%s) %u.%u.%u.%u\n",
477 ifname, NIPQUAD(addr));
478
479 /* Now bind the socket with the address of multicast interface */
480 sin.sin_family = AF_INET;
481 sin.sin_addr.s_addr = addr;
482 sin.sin_port = 0;
483
484 return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
485}
486
487/*
488 * Set up sending multicast socket over UDP
489 */
490static struct socket * make_send_sock(void)
491{
492 struct socket *sock;
493
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");
497 return NULL;
498 }
499
500 if (set_mcast_if(sock->sk, ip_vs_master_mcast_ifn) < 0) {
501 IP_VS_ERR("Error setting outbound mcast interface\n");
502 goto error;
503 }
504
505 set_mcast_loop(sock->sk, 0);
506 set_mcast_ttl(sock->sk, 1);
507
508 if (bind_mcastif_addr(sock, ip_vs_master_mcast_ifn) < 0) {
509 IP_VS_ERR("Error binding address of the mcast interface\n");
510 goto error;
511 }
512
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");
517 goto error;
518 }
519
520 return sock;
521
522 error:
523 sock_release(sock);
524 return NULL;
525}
526
527
528/*
529 * Set up receiving multicast socket over UDP
530 */
531static struct socket * make_receive_sock(void)
532{
533 struct socket *sock;
534
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");
538 return NULL;
539 }
540
541 /* it is equivalent to the REUSEADDR option in user-space */
542 sock->sk->sk_reuse = 1;
543
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");
548 goto error;
549 }
550
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");
556 goto error;
557 }
558
559 return sock;
560
561 error:
562 sock_release(sock);
563 return NULL;
564}
565
566
567static int
568ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
569{
570 struct msghdr msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
571 struct kvec iov;
572 int len;
573
574 EnterFunction(7);
575 iov.iov_base = (void *)buffer;
576 iov.iov_len = length;
577
578 len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
579
580 LeaveFunction(7);
581 return len;
582}
583
584static void
585ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
586{
587 int msize;
588
589 msize = msg->size;
590
591 /* Put size in network byte order */
592 msg->size = htons(msg->size);
593
594 if (ip_vs_send_async(sock, (char *)msg, msize) != msize)
595 IP_VS_ERR("ip_vs_send_async error\n");
596}
597
598static int
599ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
600{
601 struct msghdr msg = {NULL,};
602 struct kvec iov;
603 int len;
604
605 EnterFunction(7);
606
607 /* Receive a packet */
608 iov.iov_base = buffer;
609 iov.iov_len = (size_t)buflen;
610
611 len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, 0);
612
613 if (len < 0)
614 return -1;
615
616 LeaveFunction(7);
617 return len;
618}
619
620
621static DECLARE_WAIT_QUEUE_HEAD(sync_wait);
622static pid_t sync_master_pid = 0;
623static pid_t sync_backup_pid = 0;
624
625static DECLARE_WAIT_QUEUE_HEAD(stop_sync_wait);
626static int stop_master_sync = 0;
627static int stop_backup_sync = 0;
628
629static void sync_master_loop(void)
630{
631 struct socket *sock;
632 struct ip_vs_sync_buff *sb;
633
634 /* create the sending multicast socket */
635 sock = make_send_sock();
636 if (!sock)
637 return;
638
639 IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, "
640 "syncid = %d\n",
641 ip_vs_master_mcast_ifn, ip_vs_master_syncid);
642
643 for (;;) {
644 while ((sb=sb_dequeue())) {
645 ip_vs_send_sync_msg(sock, sb->mesg);
646 ip_vs_sync_buff_release(sb);
647 }
648
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);
653 }
654
655 if (stop_master_sync)
656 break;
657
658 ssleep(1);
659 }
660
661 /* clean up the sync_buff queue */
662 while ((sb=sb_dequeue())) {
663 ip_vs_sync_buff_release(sb);
664 }
665
666 /* clean up the current sync_buff */
667 if ((sb = get_curr_sync_buff(0))) {
668 ip_vs_sync_buff_release(sb);
669 }
670
671 /* release the sending multicast socket */
672 sock_release(sock);
673}
674
675
676static void sync_backup_loop(void)
677{
678 struct socket *sock;
679 char *buf;
680 int len;
681
682 if (!(buf = kmalloc(sync_recv_mesg_maxlen, GFP_ATOMIC))) {
683 IP_VS_ERR("sync_backup_loop: kmalloc error\n");
684 return;
685 }
686
687 /* create the receiving multicast socket */
688 sock = make_receive_sock();
689 if (!sock)
690 goto out;
691
692 IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
693 "syncid = %d\n",
694 ip_vs_backup_mcast_ifn, ip_vs_backup_syncid);
695
696 for (;;) {
697 /* do you have data now? */
698 while (!skb_queue_empty(&(sock->sk->sk_receive_queue))) {
699 if ((len =
700 ip_vs_receive(sock, buf,
701 sync_recv_mesg_maxlen)) <= 0) {
702 IP_VS_ERR("receiving message error\n");
703 break;
704 }
705 /* disable bottom half, because it accessed the data
706 shared by softirq while getting/creating conns */
707 local_bh_disable();
708 ip_vs_process_message(buf, len);
709 local_bh_enable();
710 }
711
712 if (stop_backup_sync)
713 break;
714
715 ssleep(1);
716 }
717
718 /* release the sending multicast socket */
719 sock_release(sock);
720
721 out:
722 kfree(buf);
723}
724
725
726static void set_sync_pid(int sync_state, pid_t sync_pid)
727{
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;
732}
733
734static void set_stop_sync(int sync_state, int set)
735{
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;
740 else {
741 stop_master_sync = set;
742 stop_backup_sync = set;
743 }
744}
745
746static int sync_thread(void *startup)
747{
748 DECLARE_WAITQUEUE(wait, current);
749 mm_segment_t oldmm;
750 int state;
751 const char *name;
752
753 /* increase the module use count */
754 ip_vs_use_count_inc();
755
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";
762 } else {
763 IP_VS_BUG();
764 ip_vs_use_count_dec();
765 return -EINVAL;
766 }
767
768 daemonize(name);
769
770 oldmm = get_fs();
771 set_fs(KERNEL_DS);
772
773 /* Block all signals */
774 spin_lock_irq(&current->sighand->siglock);
775 siginitsetinv(&current->blocked, 0);
776 recalc_sigpending();
777 spin_unlock_irq(&current->sighand->siglock);
778
779 /* set the maximum length of sync message */
780 set_sync_mesg_maxlen(state);
781
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);
786
787 add_wait_queue(&sync_wait, &wait);
788
789 set_sync_pid(state, current->pid);
790 complete((struct completion *)startup);
791
792 /* processing master/backup loop here */
793 if (state == IP_VS_STATE_MASTER)
794 sync_master_loop();
795 else if (state == IP_VS_STATE_BACKUP)
796 sync_backup_loop();
797 else IP_VS_BUG();
798
799 remove_wait_queue(&sync_wait, &wait);
800
801 /* thread exits */
802 set_sync_pid(state, 0);
803 IP_VS_INFO("sync thread stopped!\n");
804
805 set_fs(oldmm);
806
807 /* decrease the module use count */
808 ip_vs_use_count_dec();
809
810 set_stop_sync(state, 0);
811 wake_up(&stop_sync_wait);
812
813 return 0;
814}
815
816
817static int fork_sync_thread(void *startup)
818{
819 pid_t pid;
820
821 /* fork the sync thread here, then the parent process of the
822 sync thread is the init process after this thread exits. */
823 repeat:
824 if ((pid = kernel_thread(sync_thread, startup, 0)) < 0) {
825 IP_VS_ERR("could not create sync_thread due to %d... "
826 "retrying.\n", pid);
827 ssleep(1);
828 goto repeat;
829 }
830
831 return 0;
832}
833
834
835int start_sync_thread(int state, char *mcast_ifn, __u8 syncid)
836{
837 DECLARE_COMPLETION(startup);
838 pid_t pid;
839
840 if ((state == IP_VS_STATE_MASTER && sync_master_pid) ||
841 (state == IP_VS_STATE_BACKUP && sync_backup_pid))
842 return -EEXIST;
843
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));
847
848 ip_vs_sync_state |= state;
849 if (state == IP_VS_STATE_MASTER) {
pageexec4da62fc2005-06-26 16:00:19 -0700850 strlcpy(ip_vs_master_mcast_ifn, mcast_ifn, sizeof(ip_vs_master_mcast_ifn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 ip_vs_master_syncid = syncid;
852 } else {
pageexec4da62fc2005-06-26 16:00:19 -0700853 strlcpy(ip_vs_backup_mcast_ifn, mcast_ifn, sizeof(ip_vs_backup_mcast_ifn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 ip_vs_backup_syncid = syncid;
855 }
856
857 repeat:
858 if ((pid = kernel_thread(fork_sync_thread, &startup, 0)) < 0) {
859 IP_VS_ERR("could not create fork_sync_thread due to %d... "
860 "retrying.\n", pid);
861 ssleep(1);
862 goto repeat;
863 }
864
865 wait_for_completion(&startup);
866
867 return 0;
868}
869
870
871int stop_sync_thread(int state)
872{
873 DECLARE_WAITQUEUE(wait, current);
874
875 if ((state == IP_VS_STATE_MASTER && !sync_master_pid) ||
876 (state == IP_VS_STATE_BACKUP && !sync_backup_pid))
877 return -ESRCH;
878
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);
882
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;
887 wake_up(&sync_wait);
888 schedule();
889 __set_current_state(TASK_RUNNING);
890 remove_wait_queue(&stop_sync_wait, &wait);
891
892 /* Note: no need to reap the sync thread, because its parent
893 process is the init process */
894
895 if ((state == IP_VS_STATE_MASTER && stop_master_sync) ||
896 (state == IP_VS_STATE_BACKUP && stop_backup_sync))
897 IP_VS_BUG();
898
899 return 0;
900}