1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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>
29#include <linux/in.h>
30#include <linux/igmp.h>
31#include <linux/udp.h>
32
33#include <net/ip.h>
34#include <net/sock.h>
35#include <asm/uaccess.h>
36
37#include <net/ip_vs.h>
38
39#define IP_VS_SYNC_GROUP 0xe0000051
40#define IP_VS_SYNC_PORT 8848
41
42
43
44
45
46struct ip_vs_sync_conn {
47 __u8 reserved;
48
49
50 __u8 protocol;
51 __be16 cport;
52 __be16 vport;
53 __be16 dport;
54 __be32 caddr;
55 __be32 vaddr;
56 __be32 daddr;
57
58
59 __be16 flags;
60 __be16 state;
61
62
63};
64
65struct ip_vs_sync_conn_options {
66 struct ip_vs_seq in_seq;
67 struct ip_vs_seq out_seq;
68};
69
70struct ip_vs_sync_thread_data {
71 struct completion *startup;
72 int state;
73};
74
75#define SIMPLE_CONN_SIZE (sizeof(struct ip_vs_sync_conn))
76#define FULL_CONN_SIZE \
77(sizeof(struct ip_vs_sync_conn) + sizeof(struct ip_vs_sync_conn_options))
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101#define SYNC_MESG_HEADER_LEN 4
102
103struct ip_vs_sync_mesg {
104 __u8 nr_conns;
105 __u8 syncid;
106 __u16 size;
107
108
109};
110
111
112static int sync_send_mesg_maxlen;
113static int sync_recv_mesg_maxlen;
114
115struct ip_vs_sync_buff {
116 struct list_head list;
117 unsigned long firstuse;
118
119
120 struct ip_vs_sync_mesg *mesg;
121 unsigned char *head;
122 unsigned char *end;
123};
124
125
126
127static LIST_HEAD(ip_vs_sync_queue);
128static DEFINE_SPINLOCK(ip_vs_sync_lock);
129
130
131static struct ip_vs_sync_buff *curr_sb = NULL;
132static DEFINE_SPINLOCK(curr_sb_lock);
133
134
135volatile int ip_vs_sync_state = IP_VS_STATE_NONE;
136volatile int ip_vs_master_syncid = 0;
137volatile int ip_vs_backup_syncid = 0;
138
139
140char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
141char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
142
143
144static struct sockaddr_in mcast_addr;
145
146
147static inline void sb_queue_tail(struct ip_vs_sync_buff *sb)
148{
149 spin_lock(&ip_vs_sync_lock);
150 list_add_tail(&sb->list, &ip_vs_sync_queue);
151 spin_unlock(&ip_vs_sync_lock);
152}
153
154static inline struct ip_vs_sync_buff * sb_dequeue(void)
155{
156 struct ip_vs_sync_buff *sb;
157
158 spin_lock_bh(&ip_vs_sync_lock);
159 if (list_empty(&ip_vs_sync_queue)) {
160 sb = NULL;
161 } else {
162 sb = list_entry(ip_vs_sync_queue.next,
163 struct ip_vs_sync_buff,
164 list);
165 list_del(&sb->list);
166 }
167 spin_unlock_bh(&ip_vs_sync_lock);
168
169 return sb;
170}
171
172static inline struct ip_vs_sync_buff * ip_vs_sync_buff_create(void)
173{
174 struct ip_vs_sync_buff *sb;
175
176 if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
177 return NULL;
178
179 if (!(sb->mesg=kmalloc(sync_send_mesg_maxlen, GFP_ATOMIC))) {
180 kfree(sb);
181 return NULL;
182 }
183 sb->mesg->nr_conns = 0;
184 sb->mesg->syncid = ip_vs_master_syncid;
185 sb->mesg->size = 4;
186 sb->head = (unsigned char *)sb->mesg + 4;
187 sb->end = (unsigned char *)sb->mesg + sync_send_mesg_maxlen;
188 sb->firstuse = jiffies;
189 return sb;
190}
191
192static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff *sb)
193{
194 kfree(sb->mesg);
195 kfree(sb);
196}
197
198
199
200
201
202static inline struct ip_vs_sync_buff *
203get_curr_sync_buff(unsigned long time)
204{
205 struct ip_vs_sync_buff *sb;
206
207 spin_lock_bh(&curr_sb_lock);
208 if (curr_sb && (time == 0 ||
209 time_before(jiffies - curr_sb->firstuse, time))) {
210 sb = curr_sb;
211 curr_sb = NULL;
212 } else
213 sb = NULL;
214 spin_unlock_bh(&curr_sb_lock);
215 return sb;
216}
217
218
219
220
221
222
223void ip_vs_sync_conn(struct ip_vs_conn *cp)
224{
225 struct ip_vs_sync_mesg *m;
226 struct ip_vs_sync_conn *s;
227 int len;
228
229 spin_lock(&curr_sb_lock);
230 if (!curr_sb) {
231 if (!(curr_sb=ip_vs_sync_buff_create())) {
232 spin_unlock(&curr_sb_lock);
233 IP_VS_ERR("ip_vs_sync_buff_create failed.\n");
234 return;
235 }
236 }
237
238 len = (cp->flags & IP_VS_CONN_F_SEQ_MASK) ? FULL_CONN_SIZE :
239 SIMPLE_CONN_SIZE;
240 m = curr_sb->mesg;
241 s = (struct ip_vs_sync_conn *)curr_sb->head;
242
243
244 s->protocol = cp->protocol;
245 s->cport = cp->cport;
246 s->vport = cp->vport;
247 s->dport = cp->dport;
248 s->caddr = cp->caddr;
249 s->vaddr = cp->vaddr;
250 s->daddr = cp->daddr;
251 s->flags = htons(cp->flags & ~IP_VS_CONN_F_HASHED);
252 s->state = htons(cp->state);
253 if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
254 struct ip_vs_sync_conn_options *opt =
255 (struct ip_vs_sync_conn_options *)&s[1];
256 memcpy(opt, &cp->in_seq, sizeof(*opt));
257 }
258
259 m->nr_conns++;
260 m->size += len;
261 curr_sb->head += len;
262
263
264 if (curr_sb->head+FULL_CONN_SIZE > curr_sb->end) {
265 sb_queue_tail(curr_sb);
266 curr_sb = NULL;
267 }
268 spin_unlock(&curr_sb_lock);
269
270
271 if (cp->control)
272 ip_vs_sync_conn(cp->control);
273}
274
275
276
277
278
279
280static void ip_vs_process_message(const char *buffer, const size_t buflen)
281{
282 struct ip_vs_sync_mesg *m = (struct ip_vs_sync_mesg *)buffer;
283 struct ip_vs_sync_conn *s;
284 struct ip_vs_sync_conn_options *opt;
285 struct ip_vs_conn *cp;
286 struct ip_vs_protocol *pp;
287 struct ip_vs_dest *dest;
288 char *p;
289 int i;
290
291 if (buflen < sizeof(struct ip_vs_sync_mesg)) {
292 IP_VS_ERR_RL("sync message header too short\n");
293 return;
294 }
295
296
297 m->size = ntohs(m->size);
298
299 if (buflen != m->size) {
300 IP_VS_ERR_RL("bogus sync message size\n");
301 return;
302 }
303
304
305 if (ip_vs_backup_syncid != 0 && m->syncid != ip_vs_backup_syncid) {
306 IP_VS_DBG(7, "Ignoring incoming msg with syncid = %d\n",
307 m->syncid);
308 return;
309 }
310
311 p = (char *)buffer + sizeof(struct ip_vs_sync_mesg);
312 for (i=0; i<m->nr_conns; i++) {
313 unsigned flags, state;
314
315 if (p + SIMPLE_CONN_SIZE > buffer+buflen) {
316 IP_VS_ERR_RL("bogus conn in sync message\n");
317 return;
318 }
319 s = (struct ip_vs_sync_conn *) p;
320 flags = ntohs(s->flags) | IP_VS_CONN_F_SYNC;
321 flags &= ~IP_VS_CONN_F_HASHED;
322 if (flags & IP_VS_CONN_F_SEQ_MASK) {
323 opt = (struct ip_vs_sync_conn_options *)&s[1];
324 p += FULL_CONN_SIZE;
325 if (p > buffer+buflen) {
326 IP_VS_ERR_RL("bogus conn options in sync message\n");
327 return;
328 }
329 } else {
330 opt = NULL;
331 p += SIMPLE_CONN_SIZE;
332 }
333
334 state = ntohs(s->state);
335 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
336 pp = ip_vs_proto_get(s->protocol);
337 if (!pp) {
338 IP_VS_ERR_RL("Unsupported protocol %u in sync msg\n",
339 s->protocol);
340 continue;
341 }
342 if (state >= pp->num_states) {
343 IP_VS_DBG(2, "Invalid %s state %u in sync msg\n",
344 pp->name, state);
345 continue;
346 }
347 } else {
348
349 pp = NULL;
350 if (state > 0) {
351 IP_VS_DBG(2, "Invalid template state %u in sync msg\n",
352 state);
353 state = 0;
354 }
355 }
356
357 if (!(flags & IP_VS_CONN_F_TEMPLATE))
358 cp = ip_vs_conn_in_get(s->protocol,
359 s->caddr, s->cport,
360 s->vaddr, s->vport);
361 else
362 cp = ip_vs_ct_in_get(s->protocol,
363 s->caddr, s->cport,
364 s->vaddr, s->vport);
365 if (!cp) {
366
367
368
369
370
371 dest = ip_vs_find_dest(s->daddr, s->dport,
372 s->vaddr, s->vport,
373 s->protocol);
374
375 if (s->protocol == IPPROTO_TCP) {
376 if (state != IP_VS_TCP_S_ESTABLISHED)
377 flags |= IP_VS_CONN_F_INACTIVE;
378 else
379 flags &= ~IP_VS_CONN_F_INACTIVE;
380 }
381 cp = ip_vs_conn_new(s->protocol,
382 s->caddr, s->cport,
383 s->vaddr, s->vport,
384 s->daddr, s->dport,
385 flags, dest);
386 if (dest)
387 atomic_dec(&dest->refcnt);
388 if (!cp) {
389 IP_VS_ERR("ip_vs_conn_new failed\n");
390 return;
391 }
392 } else if (!cp->dest) {
393 dest = ip_vs_try_bind_dest(cp);
394 if (dest)
395 atomic_dec(&dest->refcnt);
396 } else if ((cp->dest) && (cp->protocol == IPPROTO_TCP) &&
397 (cp->state != state)) {
398
399 dest = cp->dest;
400 if (!(cp->flags & IP_VS_CONN_F_INACTIVE) &&
401 (state != IP_VS_TCP_S_ESTABLISHED)) {
402 atomic_dec(&dest->activeconns);
403 atomic_inc(&dest->inactconns);
404 cp->flags |= IP_VS_CONN_F_INACTIVE;
405 } else if ((cp->flags & IP_VS_CONN_F_INACTIVE) &&
406 (state == IP_VS_TCP_S_ESTABLISHED)) {
407 atomic_inc(&dest->activeconns);
408 atomic_dec(&dest->inactconns);
409 cp->flags &= ~IP_VS_CONN_F_INACTIVE;
410 }
411 }
412
413 if (opt)
414 memcpy(&cp->in_seq, opt, sizeof(*opt));
415 atomic_set(&cp->in_pkts, sysctl_ip_vs_sync_threshold[0]);
416 cp->state = state;
417 cp->old_state = cp->state;
418
419
420
421
422
423
424 if (!(flags & IP_VS_CONN_F_TEMPLATE) && pp->timeout_table)
425 cp->timeout = pp->timeout_table[state];
426 else
427 cp->timeout = (3*60*HZ);
428 ip_vs_conn_put(cp);
429 }
430}
431
432
433
434
435
436static void set_mcast_loop(struct sock *sk, u_char loop)
437{
438 struct inet_sock *inet = inet_sk(sk);
439
440
441 lock_sock(sk);
442 inet->mc_loop = loop ? 1 : 0;
443 release_sock(sk);
444}
445
446
447
448
449static void set_mcast_ttl(struct sock *sk, u_char ttl)
450{
451 struct inet_sock *inet = inet_sk(sk);
452
453
454 lock_sock(sk);
455 inet->mc_ttl = ttl;
456 release_sock(sk);
457}
458
459
460
461
462static int set_mcast_if(struct sock *sk, char *ifname)
463{
464 struct net_device *dev;
465 struct inet_sock *inet = inet_sk(sk);
466
467 if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
468 return -ENODEV;
469
470 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
471 return -EINVAL;
472
473 lock_sock(sk);
474 inet->mc_index = dev->ifindex;
475
476 release_sock(sk);
477
478 return 0;
479}
480
481
482
483
484
485
486static int set_sync_mesg_maxlen(int sync_state)
487{
488 struct net_device *dev;
489 int num;
490
491 if (sync_state == IP_VS_STATE_MASTER) {
492 if ((dev = __dev_get_by_name(&init_net, ip_vs_master_mcast_ifn)) == NULL)
493 return -ENODEV;
494
495 num = (dev->mtu - sizeof(struct iphdr) -
496 sizeof(struct udphdr) -
497 SYNC_MESG_HEADER_LEN - 20) / SIMPLE_CONN_SIZE;
498 sync_send_mesg_maxlen =
499 SYNC_MESG_HEADER_LEN + SIMPLE_CONN_SIZE * num;
500 IP_VS_DBG(7, "setting the maximum length of sync sending "
501 "message %d.\n", sync_send_mesg_maxlen);
502 } else if (sync_state == IP_VS_STATE_BACKUP) {
503 if ((dev = __dev_get_by_name(&init_net, ip_vs_backup_mcast_ifn)) == NULL)
504 return -ENODEV;
505
506 sync_recv_mesg_maxlen = dev->mtu -
507 sizeof(struct iphdr) - sizeof(struct udphdr);
508 IP_VS_DBG(7, "setting the maximum length of sync receiving "
509 "message %d.\n", sync_recv_mesg_maxlen);
510 }
511
512 return 0;
513}
514
515
516
517
518
519
520
521static int
522join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
523{
524 struct ip_mreqn mreq;
525 struct net_device *dev;
526 int ret;
527
528 memset(&mreq, 0, sizeof(mreq));
529 memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
530
531 if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
532 return -ENODEV;
533 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
534 return -EINVAL;
535
536 mreq.imr_ifindex = dev->ifindex;
537
538 lock_sock(sk);
539 ret = ip_mc_join_group(sk, &mreq);
540 release_sock(sk);
541
542 return ret;
543}
544
545
546static int bind_mcastif_addr(struct socket *sock, char *ifname)
547{
548 struct net_device *dev;
549 __be32 addr;
550 struct sockaddr_in sin;
551
552 if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
553 return -ENODEV;
554
555 addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
556 if (!addr)
557 IP_VS_ERR("You probably need to specify IP address on "
558 "multicast interface.\n");
559
560 IP_VS_DBG(7, "binding socket with (%s) %u.%u.%u.%u\n",
561 ifname, NIPQUAD(addr));
562
563
564 sin.sin_family = AF_INET;
565 sin.sin_addr.s_addr = addr;
566 sin.sin_port = 0;
567
568 return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
569}
570
571
572
573
574static struct socket * make_send_sock(void)
575{
576 struct socket *sock;
577
578
579 if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
580 IP_VS_ERR("Error during creation of socket; terminating\n");
581 return NULL;
582 }
583
584 if (set_mcast_if(sock->sk, ip_vs_master_mcast_ifn) < 0) {
585 IP_VS_ERR("Error setting outbound mcast interface\n");
586 goto error;
587 }
588
589 set_mcast_loop(sock->sk, 0);
590 set_mcast_ttl(sock->sk, 1);
591
592 if (bind_mcastif_addr(sock, ip_vs_master_mcast_ifn) < 0) {
593 IP_VS_ERR("Error binding address of the mcast interface\n");
594 goto error;
595 }
596
597 if (sock->ops->connect(sock,
598 (struct sockaddr*)&mcast_addr,
599 sizeof(struct sockaddr), 0) < 0) {
600 IP_VS_ERR("Error connecting to the multicast addr\n");
601 goto error;
602 }
603
604 return sock;
605
606 error:
607 sock_release(sock);
608 return NULL;
609}
610
611
612
613
614
615static struct socket * make_receive_sock(void)
616{
617 struct socket *sock;
618
619
620 if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
621 IP_VS_ERR("Error during creation of socket; terminating\n");
622 return NULL;
623 }
624
625
626 sock->sk->sk_reuse = 1;
627
628 if (sock->ops->bind(sock,
629 (struct sockaddr*)&mcast_addr,
630 sizeof(struct sockaddr)) < 0) {
631 IP_VS_ERR("Error binding to the multicast addr\n");
632 goto error;
633 }
634
635
636 if (join_mcast_group(sock->sk,
637 (struct in_addr*)&mcast_addr.sin_addr,
638 ip_vs_backup_mcast_ifn) < 0) {
639 IP_VS_ERR("Error joining to the multicast group\n");
640 goto error;
641 }
642
643 return sock;
644
645 error:
646 sock_release(sock);
647 return NULL;
648}
649
650
651static int
652ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
653{
654 struct msghdr msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
655 struct kvec iov;
656 int len;
657
658 EnterFunction(7);
659 iov.iov_base = (void *)buffer;
660 iov.iov_len = length;
661
662 len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
663
664 LeaveFunction(7);
665 return len;
666}
667
668static void
669ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
670{
671 int msize;
672
673 msize = msg->size;
674
675
676 msg->size = htons(msg->size);
677
678 if (ip_vs_send_async(sock, (char *)msg, msize) != msize)
679 IP_VS_ERR("ip_vs_send_async error\n");
680}
681
682static int
683ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
684{
685 struct msghdr msg = {NULL,};
686 struct kvec iov;
687 int len;
688
689 EnterFunction(7);
690
691
692 iov.iov_base = buffer;
693 iov.iov_len = (size_t)buflen;
694
695 len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, 0);
696
697 if (len < 0)
698 return -1;
699
700 LeaveFunction(7);
701 return len;
702}
703
704
705static DECLARE_WAIT_QUEUE_HEAD(sync_wait);
706static pid_t sync_master_pid = 0;
707static pid_t sync_backup_pid = 0;
708
709static DECLARE_WAIT_QUEUE_HEAD(stop_sync_wait);
710static int stop_master_sync = 0;
711static int stop_backup_sync = 0;
712
713static void sync_master_loop(void)
714{
715 struct socket *sock;
716 struct ip_vs_sync_buff *sb;
717
718
719 sock = make_send_sock();
720 if (!sock)
721 return;
722
723 IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, "
724 "syncid = %d\n",
725 ip_vs_master_mcast_ifn, ip_vs_master_syncid);
726
727 for (;;) {
728 while ((sb=sb_dequeue())) {
729 ip_vs_send_sync_msg(sock, sb->mesg);
730 ip_vs_sync_buff_release(sb);
731 }
732
733
734 if ((sb = get_curr_sync_buff(2*HZ))) {
735 ip_vs_send_sync_msg(sock, sb->mesg);
736 ip_vs_sync_buff_release(sb);
737 }
738
739 if (stop_master_sync)
740 break;
741
742 msleep_interruptible(1000);
743 }
744
745
746 while ((sb=sb_dequeue())) {
747 ip_vs_sync_buff_release(sb);
748 }
749
750
751 if ((sb = get_curr_sync_buff(0))) {
752 ip_vs_sync_buff_release(sb);
753 }
754
755
756 sock_release(sock);
757}
758
759
760static void sync_backup_loop(void)
761{
762 struct socket *sock;
763 char *buf;
764 int len;
765
766 if (!(buf = kmalloc(sync_recv_mesg_maxlen, GFP_ATOMIC))) {
767 IP_VS_ERR("sync_backup_loop: kmalloc error\n");
768 return;
769 }
770
771
772 sock = make_receive_sock();
773 if (!sock)
774 goto out;
775
776 IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
777 "syncid = %d\n",
778 ip_vs_backup_mcast_ifn, ip_vs_backup_syncid);
779
780 for (;;) {
781
782 while (!skb_queue_empty(&(sock->sk->sk_receive_queue))) {
783 if ((len =
784 ip_vs_receive(sock, buf,
785 sync_recv_mesg_maxlen)) <= 0) {
786 IP_VS_ERR("receiving message error\n");
787 break;
788 }
789
790
791 local_bh_disable();
792 ip_vs_process_message(buf, len);
793 local_bh_enable();
794 }
795
796 if (stop_backup_sync)
797 break;
798
799 msleep_interruptible(1000);
800 }
801
802
803 sock_release(sock);
804
805 out:
806 kfree(buf);
807}
808
809
810static void set_sync_pid(int sync_state, pid_t sync_pid)
811{
812 if (sync_state == IP_VS_STATE_MASTER)
813 sync_master_pid = sync_pid;
814 else if (sync_state == IP_VS_STATE_BACKUP)
815 sync_backup_pid = sync_pid;
816}
817
818static void set_stop_sync(int sync_state, int set)
819{
820 if (sync_state == IP_VS_STATE_MASTER)
821 stop_master_sync = set;
822 else if (sync_state == IP_VS_STATE_BACKUP)
823 stop_backup_sync = set;
824 else {
825 stop_master_sync = set;
826 stop_backup_sync = set;
827 }
828}
829
830static int sync_thread(void *startup)
831{
832 DECLARE_WAITQUEUE(wait, current);
833 mm_segment_t oldmm;
834 int state;
835 const char *name;
836 struct ip_vs_sync_thread_data *tinfo = startup;
837
838
839 ip_vs_use_count_inc();
840
841 if (ip_vs_sync_state & IP_VS_STATE_MASTER && !sync_master_pid) {
842 state = IP_VS_STATE_MASTER;
843 name = "ipvs_syncmaster";
844 } else if (ip_vs_sync_state & IP_VS_STATE_BACKUP && !sync_backup_pid) {
845 state = IP_VS_STATE_BACKUP;
846 name = "ipvs_syncbackup";
847 } else {
848 IP_VS_BUG();
849 ip_vs_use_count_dec();
850 return -EINVAL;
851 }
852
853 daemonize(name);
854
855 oldmm = get_fs();
856 set_fs(KERNEL_DS);
857
858
859 spin_lock_irq(¤t->sighand->siglock);
860 siginitsetinv(¤t->blocked, 0);
861 recalc_sigpending();
862 spin_unlock_irq(¤t->sighand->siglock);
863
864
865 set_sync_mesg_maxlen(state);
866
867
868 mcast_addr.sin_family = AF_INET;
869 mcast_addr.sin_port = htons(IP_VS_SYNC_PORT);
870 mcast_addr.sin_addr.s_addr = htonl(IP_VS_SYNC_GROUP);
871
872 add_wait_queue(&sync_wait, &wait);
873
874 set_sync_pid(state, task_pid_nr(current));
875 complete(tinfo->startup);
876
877
878
879
880
881
882 tinfo->startup = NULL;
883
884
885 if (state == IP_VS_STATE_MASTER)
886 sync_master_loop();
887 else if (state == IP_VS_STATE_BACKUP)
888 sync_backup_loop();
889 else IP_VS_BUG();
890
891 remove_wait_queue(&sync_wait, &wait);
892
893
894
895
896
897
898
899 if ((!stop_master_sync) && (!stop_backup_sync))
900 ip_vs_sync_state -= tinfo->state;
901
902 set_sync_pid(state, 0);
903 IP_VS_INFO("sync thread stopped!\n");
904
905 set_fs(oldmm);
906
907
908 ip_vs_use_count_dec();
909
910 set_stop_sync(state, 0);
911 wake_up(&stop_sync_wait);
912
913
914
915
916
917 kfree(tinfo);
918 return 0;
919}
920
921
922static int fork_sync_thread(void *startup)
923{
924 pid_t pid;
925
926
927
928 repeat:
929 if ((pid = kernel_thread(sync_thread, startup, 0)) < 0) {
930 IP_VS_ERR("could not create sync_thread due to %d... "
931 "retrying.\n", pid);
932 msleep_interruptible(1000);
933 goto repeat;
934 }
935
936 return 0;
937}
938
939
940int start_sync_thread(int state, char *mcast_ifn, __u8 syncid)
941{
942 DECLARE_COMPLETION_ONSTACK(startup);
943 pid_t pid;
944 struct ip_vs_sync_thread_data *tinfo;
945
946 if ((state == IP_VS_STATE_MASTER && sync_master_pid) ||
947 (state == IP_VS_STATE_BACKUP && sync_backup_pid))
948 return -EEXIST;
949
950
951
952
953 tinfo = kmalloc(sizeof(struct ip_vs_sync_thread_data), GFP_KERNEL);
954 if (!tinfo)
955 return -ENOMEM;
956
957 IP_VS_DBG(7, "%s: pid %d\n", __func__, task_pid_nr(current));
958 IP_VS_DBG(7, "Each ip_vs_sync_conn entry need %Zd bytes\n",
959 sizeof(struct ip_vs_sync_conn));
960
961 ip_vs_sync_state |= state;
962 if (state == IP_VS_STATE_MASTER) {
963 strlcpy(ip_vs_master_mcast_ifn, mcast_ifn,
964 sizeof(ip_vs_master_mcast_ifn));
965 ip_vs_master_syncid = syncid;
966 } else {
967 strlcpy(ip_vs_backup_mcast_ifn, mcast_ifn,
968 sizeof(ip_vs_backup_mcast_ifn));
969 ip_vs_backup_syncid = syncid;
970 }
971
972 tinfo->state = state;
973 tinfo->startup = &startup;
974
975 repeat:
976 if ((pid = kernel_thread(fork_sync_thread, tinfo, 0)) < 0) {
977 IP_VS_ERR("could not create fork_sync_thread due to %d... "
978 "retrying.\n", pid);
979 msleep_interruptible(1000);
980 goto repeat;
981 }
982
983 wait_for_completion(&startup);
984
985 return 0;
986}
987
988
989int stop_sync_thread(int state)
990{
991 DECLARE_WAITQUEUE(wait, current);
992
993 if ((state == IP_VS_STATE_MASTER && !sync_master_pid) ||
994 (state == IP_VS_STATE_BACKUP && !sync_backup_pid))
995 return -ESRCH;
996
997 IP_VS_DBG(7, "%s: pid %d\n", __func__, task_pid_nr(current));
998 IP_VS_INFO("stopping sync thread %d ...\n",
999 (state == IP_VS_STATE_MASTER) ?
1000 sync_master_pid : sync_backup_pid);
1001
1002 __set_current_state(TASK_UNINTERRUPTIBLE);
1003 add_wait_queue(&stop_sync_wait, &wait);
1004 set_stop_sync(state, 1);
1005 ip_vs_sync_state -= state;
1006 wake_up(&sync_wait);
1007 schedule();
1008 __set_current_state(TASK_RUNNING);
1009 remove_wait_queue(&stop_sync_wait, &wait);
1010
1011
1012
1013
1014 if ((state == IP_VS_STATE_MASTER && stop_master_sync) ||
1015 (state == IP_VS_STATE_BACKUP && stop_backup_sync))
1016 IP_VS_BUG();
1017
1018 return 0;
1019}
1020