Merge master.kernel.org:/pub/scm/linux/kernel/git/jejb/scsi-rc-fixes-2.6
[GitHub/moto-9609/android_kernel_motorola_exynos9610.git] / net / ipv4 / ipvs / ip_vs_sync.c
1 /*
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/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> /* for ip_mc_join_group */
31 #include <linux/udp.h>
32
33 #include <net/ip.h>
34 #include <net/sock.h>
35 #include <asm/uaccess.h> /* for get_fs and set_fs */
36
37 #include <net/ip_vs.h>
38
39 #define IP_VS_SYNC_GROUP 0xe0000051 /* multicast addr - 224.0.0.81 */
40 #define IP_VS_SYNC_PORT 8848 /* multicast port */
41
42
43 /*
44 * IPVS sync connection entry
45 */
46 struct ip_vs_sync_conn {
47 __u8 reserved;
48
49 /* Protocol, addresses and port numbers */
50 __u8 protocol; /* Which protocol (TCP/UDP) */
51 __be16 cport;
52 __be16 vport;
53 __be16 dport;
54 __be32 caddr; /* client address */
55 __be32 vaddr; /* virtual address */
56 __be32 daddr; /* destination address */
57
58 /* Flags and state transition */
59 __be16 flags; /* status flags */
60 __be16 state; /* state info */
61
62 /* The sequence options start here */
63 };
64
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 */
68 };
69
70 struct 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 The master mulitcasts messages to the backup load balancers in the
82 following format.
83
84 0 1 2 3
85 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
86 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
87 | Count Conns | SyncID | Size |
88 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
89 | |
90 | IPVS Sync Connection (1) |
91 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
92 | . |
93 | . |
94 | . |
95 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96 | |
97 | IPVS Sync Connection (n) |
98 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99 */
100
101 #define SYNC_MESG_HEADER_LEN 4
102
103 struct ip_vs_sync_mesg {
104 __u8 nr_conns;
105 __u8 syncid;
106 __u16 size;
107
108 /* ip_vs_sync_conn entries start here */
109 };
110
111 /* the maximum length of sync (sending/receiving) message */
112 static int sync_send_mesg_maxlen;
113 static int sync_recv_mesg_maxlen;
114
115 struct ip_vs_sync_buff {
116 struct list_head list;
117 unsigned long firstuse;
118
119 /* pointers for the message data */
120 struct ip_vs_sync_mesg *mesg;
121 unsigned char *head;
122 unsigned char *end;
123 };
124
125
126 /* the sync_buff list head and the lock */
127 static LIST_HEAD(ip_vs_sync_queue);
128 static DEFINE_SPINLOCK(ip_vs_sync_lock);
129
130 /* current sync_buff for accepting new conn entries */
131 static struct ip_vs_sync_buff *curr_sb = NULL;
132 static DEFINE_SPINLOCK(curr_sb_lock);
133
134 /* ipvs sync daemon state */
135 volatile int ip_vs_sync_state = IP_VS_STATE_NONE;
136 volatile int ip_vs_master_syncid = 0;
137 volatile int ip_vs_backup_syncid = 0;
138
139 /* multicast interface name */
140 char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
141 char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
142
143 /* multicast addr */
144 static struct sockaddr_in mcast_addr;
145
146
147 static 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
154 static 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
172 static 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
192 static 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 * Get the current sync buffer if it has been created for more
200 * than the specified time or the specified time is zero.
201 */
202 static inline struct ip_vs_sync_buff *
203 get_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 * Add an ip_vs_conn information into the current sync_buff.
221 * Called by ip_vs_in.
222 */
223 void 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 /* copy members */
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 /* check if there is a space for next one */
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 /* synchronize its controller if it has */
271 if (cp->control)
272 ip_vs_sync_conn(cp->control);
273 }
274
275
276 /*
277 * Process received multicast message and create the corresponding
278 * ip_vs_conn entries.
279 */
280 static 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 char *p;
288 int i;
289
290 /* Convert size back to host byte order */
291 m->size = ntohs(m->size);
292
293 if (buflen != m->size) {
294 IP_VS_ERR("bogus message\n");
295 return;
296 }
297
298 /* SyncID sanity check */
299 if (ip_vs_backup_syncid != 0 && m->syncid != ip_vs_backup_syncid) {
300 IP_VS_DBG(7, "Ignoring incoming msg with syncid = %d\n",
301 m->syncid);
302 return;
303 }
304
305 p = (char *)buffer + sizeof(struct ip_vs_sync_mesg);
306 for (i=0; i<m->nr_conns; i++) {
307 unsigned flags;
308
309 s = (struct ip_vs_sync_conn *)p;
310 flags = ntohs(s->flags);
311 if (!(flags & IP_VS_CONN_F_TEMPLATE))
312 cp = ip_vs_conn_in_get(s->protocol,
313 s->caddr, s->cport,
314 s->vaddr, s->vport);
315 else
316 cp = ip_vs_ct_in_get(s->protocol,
317 s->caddr, s->cport,
318 s->vaddr, s->vport);
319 if (!cp) {
320 cp = ip_vs_conn_new(s->protocol,
321 s->caddr, s->cport,
322 s->vaddr, s->vport,
323 s->daddr, s->dport,
324 flags, NULL);
325 if (!cp) {
326 IP_VS_ERR("ip_vs_conn_new failed\n");
327 return;
328 }
329 cp->state = ntohs(s->state);
330 } else if (!cp->dest) {
331 /* it is an entry created by the synchronization */
332 cp->state = ntohs(s->state);
333 cp->flags = flags | IP_VS_CONN_F_HASHED;
334 } /* Note that we don't touch its state and flags
335 if it is a normal entry. */
336
337 if (flags & IP_VS_CONN_F_SEQ_MASK) {
338 opt = (struct ip_vs_sync_conn_options *)&s[1];
339 memcpy(&cp->in_seq, opt, sizeof(*opt));
340 p += FULL_CONN_SIZE;
341 } else
342 p += SIMPLE_CONN_SIZE;
343
344 atomic_set(&cp->in_pkts, sysctl_ip_vs_sync_threshold[0]);
345 pp = ip_vs_proto_get(s->protocol);
346 cp->timeout = pp->timeout_table[cp->state];
347 ip_vs_conn_put(cp);
348
349 if (p > buffer+buflen) {
350 IP_VS_ERR("bogus message\n");
351 return;
352 }
353 }
354 }
355
356
357 /*
358 * Setup loopback of outgoing multicasts on a sending socket
359 */
360 static void set_mcast_loop(struct sock *sk, u_char loop)
361 {
362 struct inet_sock *inet = inet_sk(sk);
363
364 /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
365 lock_sock(sk);
366 inet->mc_loop = loop ? 1 : 0;
367 release_sock(sk);
368 }
369
370 /*
371 * Specify TTL for outgoing multicasts on a sending socket
372 */
373 static void set_mcast_ttl(struct sock *sk, u_char ttl)
374 {
375 struct inet_sock *inet = inet_sk(sk);
376
377 /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
378 lock_sock(sk);
379 inet->mc_ttl = ttl;
380 release_sock(sk);
381 }
382
383 /*
384 * Specifiy default interface for outgoing multicasts
385 */
386 static int set_mcast_if(struct sock *sk, char *ifname)
387 {
388 struct net_device *dev;
389 struct inet_sock *inet = inet_sk(sk);
390
391 if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
392 return -ENODEV;
393
394 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
395 return -EINVAL;
396
397 lock_sock(sk);
398 inet->mc_index = dev->ifindex;
399 /* inet->mc_addr = 0; */
400 release_sock(sk);
401
402 return 0;
403 }
404
405
406 /*
407 * Set the maximum length of sync message according to the
408 * specified interface's MTU.
409 */
410 static int set_sync_mesg_maxlen(int sync_state)
411 {
412 struct net_device *dev;
413 int num;
414
415 if (sync_state == IP_VS_STATE_MASTER) {
416 if ((dev = __dev_get_by_name(&init_net, ip_vs_master_mcast_ifn)) == NULL)
417 return -ENODEV;
418
419 num = (dev->mtu - sizeof(struct iphdr) -
420 sizeof(struct udphdr) -
421 SYNC_MESG_HEADER_LEN - 20) / SIMPLE_CONN_SIZE;
422 sync_send_mesg_maxlen =
423 SYNC_MESG_HEADER_LEN + SIMPLE_CONN_SIZE * num;
424 IP_VS_DBG(7, "setting the maximum length of sync sending "
425 "message %d.\n", sync_send_mesg_maxlen);
426 } else if (sync_state == IP_VS_STATE_BACKUP) {
427 if ((dev = __dev_get_by_name(&init_net, ip_vs_backup_mcast_ifn)) == NULL)
428 return -ENODEV;
429
430 sync_recv_mesg_maxlen = dev->mtu -
431 sizeof(struct iphdr) - sizeof(struct udphdr);
432 IP_VS_DBG(7, "setting the maximum length of sync receiving "
433 "message %d.\n", sync_recv_mesg_maxlen);
434 }
435
436 return 0;
437 }
438
439
440 /*
441 * Join a multicast group.
442 * the group is specified by a class D multicast address 224.0.0.0/8
443 * in the in_addr structure passed in as a parameter.
444 */
445 static int
446 join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
447 {
448 struct ip_mreqn mreq;
449 struct net_device *dev;
450 int ret;
451
452 memset(&mreq, 0, sizeof(mreq));
453 memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
454
455 if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
456 return -ENODEV;
457 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
458 return -EINVAL;
459
460 mreq.imr_ifindex = dev->ifindex;
461
462 lock_sock(sk);
463 ret = ip_mc_join_group(sk, &mreq);
464 release_sock(sk);
465
466 return ret;
467 }
468
469
470 static int bind_mcastif_addr(struct socket *sock, char *ifname)
471 {
472 struct net_device *dev;
473 __be32 addr;
474 struct sockaddr_in sin;
475
476 if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
477 return -ENODEV;
478
479 addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
480 if (!addr)
481 IP_VS_ERR("You probably need to specify IP address on "
482 "multicast interface.\n");
483
484 IP_VS_DBG(7, "binding socket with (%s) %u.%u.%u.%u\n",
485 ifname, NIPQUAD(addr));
486
487 /* Now bind the socket with the address of multicast interface */
488 sin.sin_family = AF_INET;
489 sin.sin_addr.s_addr = addr;
490 sin.sin_port = 0;
491
492 return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
493 }
494
495 /*
496 * Set up sending multicast socket over UDP
497 */
498 static struct socket * make_send_sock(void)
499 {
500 struct socket *sock;
501
502 /* First create a socket */
503 if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
504 IP_VS_ERR("Error during creation of socket; terminating\n");
505 return NULL;
506 }
507
508 if (set_mcast_if(sock->sk, ip_vs_master_mcast_ifn) < 0) {
509 IP_VS_ERR("Error setting outbound mcast interface\n");
510 goto error;
511 }
512
513 set_mcast_loop(sock->sk, 0);
514 set_mcast_ttl(sock->sk, 1);
515
516 if (bind_mcastif_addr(sock, ip_vs_master_mcast_ifn) < 0) {
517 IP_VS_ERR("Error binding address of the mcast interface\n");
518 goto error;
519 }
520
521 if (sock->ops->connect(sock,
522 (struct sockaddr*)&mcast_addr,
523 sizeof(struct sockaddr), 0) < 0) {
524 IP_VS_ERR("Error connecting to the multicast addr\n");
525 goto error;
526 }
527
528 return sock;
529
530 error:
531 sock_release(sock);
532 return NULL;
533 }
534
535
536 /*
537 * Set up receiving multicast socket over UDP
538 */
539 static struct socket * make_receive_sock(void)
540 {
541 struct socket *sock;
542
543 /* First create a socket */
544 if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
545 IP_VS_ERR("Error during creation of socket; terminating\n");
546 return NULL;
547 }
548
549 /* it is equivalent to the REUSEADDR option in user-space */
550 sock->sk->sk_reuse = 1;
551
552 if (sock->ops->bind(sock,
553 (struct sockaddr*)&mcast_addr,
554 sizeof(struct sockaddr)) < 0) {
555 IP_VS_ERR("Error binding to the multicast addr\n");
556 goto error;
557 }
558
559 /* join the multicast group */
560 if (join_mcast_group(sock->sk,
561 (struct in_addr*)&mcast_addr.sin_addr,
562 ip_vs_backup_mcast_ifn) < 0) {
563 IP_VS_ERR("Error joining to the multicast group\n");
564 goto error;
565 }
566
567 return sock;
568
569 error:
570 sock_release(sock);
571 return NULL;
572 }
573
574
575 static int
576 ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
577 {
578 struct msghdr msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
579 struct kvec iov;
580 int len;
581
582 EnterFunction(7);
583 iov.iov_base = (void *)buffer;
584 iov.iov_len = length;
585
586 len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
587
588 LeaveFunction(7);
589 return len;
590 }
591
592 static void
593 ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
594 {
595 int msize;
596
597 msize = msg->size;
598
599 /* Put size in network byte order */
600 msg->size = htons(msg->size);
601
602 if (ip_vs_send_async(sock, (char *)msg, msize) != msize)
603 IP_VS_ERR("ip_vs_send_async error\n");
604 }
605
606 static int
607 ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
608 {
609 struct msghdr msg = {NULL,};
610 struct kvec iov;
611 int len;
612
613 EnterFunction(7);
614
615 /* Receive a packet */
616 iov.iov_base = buffer;
617 iov.iov_len = (size_t)buflen;
618
619 len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, 0);
620
621 if (len < 0)
622 return -1;
623
624 LeaveFunction(7);
625 return len;
626 }
627
628
629 static DECLARE_WAIT_QUEUE_HEAD(sync_wait);
630 static pid_t sync_master_pid = 0;
631 static pid_t sync_backup_pid = 0;
632
633 static DECLARE_WAIT_QUEUE_HEAD(stop_sync_wait);
634 static int stop_master_sync = 0;
635 static int stop_backup_sync = 0;
636
637 static void sync_master_loop(void)
638 {
639 struct socket *sock;
640 struct ip_vs_sync_buff *sb;
641
642 /* create the sending multicast socket */
643 sock = make_send_sock();
644 if (!sock)
645 return;
646
647 IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, "
648 "syncid = %d\n",
649 ip_vs_master_mcast_ifn, ip_vs_master_syncid);
650
651 for (;;) {
652 while ((sb=sb_dequeue())) {
653 ip_vs_send_sync_msg(sock, sb->mesg);
654 ip_vs_sync_buff_release(sb);
655 }
656
657 /* check if entries stay in curr_sb for 2 seconds */
658 if ((sb = get_curr_sync_buff(2*HZ))) {
659 ip_vs_send_sync_msg(sock, sb->mesg);
660 ip_vs_sync_buff_release(sb);
661 }
662
663 if (stop_master_sync)
664 break;
665
666 msleep_interruptible(1000);
667 }
668
669 /* clean up the sync_buff queue */
670 while ((sb=sb_dequeue())) {
671 ip_vs_sync_buff_release(sb);
672 }
673
674 /* clean up the current sync_buff */
675 if ((sb = get_curr_sync_buff(0))) {
676 ip_vs_sync_buff_release(sb);
677 }
678
679 /* release the sending multicast socket */
680 sock_release(sock);
681 }
682
683
684 static void sync_backup_loop(void)
685 {
686 struct socket *sock;
687 char *buf;
688 int len;
689
690 if (!(buf = kmalloc(sync_recv_mesg_maxlen, GFP_ATOMIC))) {
691 IP_VS_ERR("sync_backup_loop: kmalloc error\n");
692 return;
693 }
694
695 /* create the receiving multicast socket */
696 sock = make_receive_sock();
697 if (!sock)
698 goto out;
699
700 IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
701 "syncid = %d\n",
702 ip_vs_backup_mcast_ifn, ip_vs_backup_syncid);
703
704 for (;;) {
705 /* do you have data now? */
706 while (!skb_queue_empty(&(sock->sk->sk_receive_queue))) {
707 if ((len =
708 ip_vs_receive(sock, buf,
709 sync_recv_mesg_maxlen)) <= 0) {
710 IP_VS_ERR("receiving message error\n");
711 break;
712 }
713 /* disable bottom half, because it accessed the data
714 shared by softirq while getting/creating conns */
715 local_bh_disable();
716 ip_vs_process_message(buf, len);
717 local_bh_enable();
718 }
719
720 if (stop_backup_sync)
721 break;
722
723 msleep_interruptible(1000);
724 }
725
726 /* release the sending multicast socket */
727 sock_release(sock);
728
729 out:
730 kfree(buf);
731 }
732
733
734 static void set_sync_pid(int sync_state, pid_t sync_pid)
735 {
736 if (sync_state == IP_VS_STATE_MASTER)
737 sync_master_pid = sync_pid;
738 else if (sync_state == IP_VS_STATE_BACKUP)
739 sync_backup_pid = sync_pid;
740 }
741
742 static void set_stop_sync(int sync_state, int set)
743 {
744 if (sync_state == IP_VS_STATE_MASTER)
745 stop_master_sync = set;
746 else if (sync_state == IP_VS_STATE_BACKUP)
747 stop_backup_sync = set;
748 else {
749 stop_master_sync = set;
750 stop_backup_sync = set;
751 }
752 }
753
754 static int sync_thread(void *startup)
755 {
756 DECLARE_WAITQUEUE(wait, current);
757 mm_segment_t oldmm;
758 int state;
759 const char *name;
760 struct ip_vs_sync_thread_data *tinfo = startup;
761
762 /* increase the module use count */
763 ip_vs_use_count_inc();
764
765 if (ip_vs_sync_state & IP_VS_STATE_MASTER && !sync_master_pid) {
766 state = IP_VS_STATE_MASTER;
767 name = "ipvs_syncmaster";
768 } else if (ip_vs_sync_state & IP_VS_STATE_BACKUP && !sync_backup_pid) {
769 state = IP_VS_STATE_BACKUP;
770 name = "ipvs_syncbackup";
771 } else {
772 IP_VS_BUG();
773 ip_vs_use_count_dec();
774 return -EINVAL;
775 }
776
777 daemonize(name);
778
779 oldmm = get_fs();
780 set_fs(KERNEL_DS);
781
782 /* Block all signals */
783 spin_lock_irq(&current->sighand->siglock);
784 siginitsetinv(&current->blocked, 0);
785 recalc_sigpending();
786 spin_unlock_irq(&current->sighand->siglock);
787
788 /* set the maximum length of sync message */
789 set_sync_mesg_maxlen(state);
790
791 /* set up multicast address */
792 mcast_addr.sin_family = AF_INET;
793 mcast_addr.sin_port = htons(IP_VS_SYNC_PORT);
794 mcast_addr.sin_addr.s_addr = htonl(IP_VS_SYNC_GROUP);
795
796 add_wait_queue(&sync_wait, &wait);
797
798 set_sync_pid(state, task_pid_nr(current));
799 complete(tinfo->startup);
800
801 /*
802 * once we call the completion queue above, we should
803 * null out that reference, since its allocated on the
804 * stack of the creating kernel thread
805 */
806 tinfo->startup = NULL;
807
808 /* processing master/backup loop here */
809 if (state == IP_VS_STATE_MASTER)
810 sync_master_loop();
811 else if (state == IP_VS_STATE_BACKUP)
812 sync_backup_loop();
813 else IP_VS_BUG();
814
815 remove_wait_queue(&sync_wait, &wait);
816
817 /* thread exits */
818
819 /*
820 * If we weren't explicitly stopped, then we
821 * exited in error, and should undo our state
822 */
823 if ((!stop_master_sync) && (!stop_backup_sync))
824 ip_vs_sync_state -= tinfo->state;
825
826 set_sync_pid(state, 0);
827 IP_VS_INFO("sync thread stopped!\n");
828
829 set_fs(oldmm);
830
831 /* decrease the module use count */
832 ip_vs_use_count_dec();
833
834 set_stop_sync(state, 0);
835 wake_up(&stop_sync_wait);
836
837 /*
838 * we need to free the structure that was allocated
839 * for us in start_sync_thread
840 */
841 kfree(tinfo);
842 return 0;
843 }
844
845
846 static int fork_sync_thread(void *startup)
847 {
848 pid_t pid;
849
850 /* fork the sync thread here, then the parent process of the
851 sync thread is the init process after this thread exits. */
852 repeat:
853 if ((pid = kernel_thread(sync_thread, startup, 0)) < 0) {
854 IP_VS_ERR("could not create sync_thread due to %d... "
855 "retrying.\n", pid);
856 msleep_interruptible(1000);
857 goto repeat;
858 }
859
860 return 0;
861 }
862
863
864 int start_sync_thread(int state, char *mcast_ifn, __u8 syncid)
865 {
866 DECLARE_COMPLETION_ONSTACK(startup);
867 pid_t pid;
868 struct ip_vs_sync_thread_data *tinfo;
869
870 if ((state == IP_VS_STATE_MASTER && sync_master_pid) ||
871 (state == IP_VS_STATE_BACKUP && sync_backup_pid))
872 return -EEXIST;
873
874 /*
875 * Note that tinfo will be freed in sync_thread on exit
876 */
877 tinfo = kmalloc(sizeof(struct ip_vs_sync_thread_data), GFP_KERNEL);
878 if (!tinfo)
879 return -ENOMEM;
880
881 IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, task_pid_nr(current));
882 IP_VS_DBG(7, "Each ip_vs_sync_conn entry need %Zd bytes\n",
883 sizeof(struct ip_vs_sync_conn));
884
885 ip_vs_sync_state |= state;
886 if (state == IP_VS_STATE_MASTER) {
887 strlcpy(ip_vs_master_mcast_ifn, mcast_ifn,
888 sizeof(ip_vs_master_mcast_ifn));
889 ip_vs_master_syncid = syncid;
890 } else {
891 strlcpy(ip_vs_backup_mcast_ifn, mcast_ifn,
892 sizeof(ip_vs_backup_mcast_ifn));
893 ip_vs_backup_syncid = syncid;
894 }
895
896 tinfo->state = state;
897 tinfo->startup = &startup;
898
899 repeat:
900 if ((pid = kernel_thread(fork_sync_thread, tinfo, 0)) < 0) {
901 IP_VS_ERR("could not create fork_sync_thread due to %d... "
902 "retrying.\n", pid);
903 msleep_interruptible(1000);
904 goto repeat;
905 }
906
907 wait_for_completion(&startup);
908
909 return 0;
910 }
911
912
913 int stop_sync_thread(int state)
914 {
915 DECLARE_WAITQUEUE(wait, current);
916
917 if ((state == IP_VS_STATE_MASTER && !sync_master_pid) ||
918 (state == IP_VS_STATE_BACKUP && !sync_backup_pid))
919 return -ESRCH;
920
921 IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, task_pid_nr(current));
922 IP_VS_INFO("stopping sync thread %d ...\n",
923 (state == IP_VS_STATE_MASTER) ?
924 sync_master_pid : sync_backup_pid);
925
926 __set_current_state(TASK_UNINTERRUPTIBLE);
927 add_wait_queue(&stop_sync_wait, &wait);
928 set_stop_sync(state, 1);
929 ip_vs_sync_state -= state;
930 wake_up(&sync_wait);
931 schedule();
932 __set_current_state(TASK_RUNNING);
933 remove_wait_queue(&stop_sync_wait, &wait);
934
935 /* Note: no need to reap the sync thread, because its parent
936 process is the init process */
937
938 if ((state == IP_VS_STATE_MASTER && stop_master_sync) ||
939 (state == IP_VS_STATE_BACKUP && stop_backup_sync))
940 IP_VS_BUG();
941
942 return 0;
943 }