IPVS: ip_vs_{un,}bind_scheduler NULL arguments
[linux-2.6-block.git] / net / netfilter / ipvs / ip_vs_sync.c
CommitLineData
1da177e4
LT
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 *
1da177e4
LT
8 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
9 *
10 * ip_vs_sync: sync connection info from master load balancer to backups
11 * through multicast
12 *
13 * Changes:
14 * Alexandre Cassen : Added master & backup support at a time.
15 * Alexandre Cassen : Added SyncID support for incoming sync
16 * messages filtering.
17 * Justin Ossevoort : Fix endian problem on sync message size.
18 */
19
9aada7ac
HE
20#define KMSG_COMPONENT "IPVS"
21#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
22
1da177e4
LT
23#include <linux/module.h>
24#include <linux/slab.h>
14c85021 25#include <linux/inetdevice.h>
1da177e4
LT
26#include <linux/net.h>
27#include <linux/completion.h>
28#include <linux/delay.h>
29#include <linux/skbuff.h>
30#include <linux/in.h>
31#include <linux/igmp.h> /* for ip_mc_join_group */
14c85021 32#include <linux/udp.h>
e6dd731c 33#include <linux/err.h>
998e7a76 34#include <linux/kthread.h>
ba6fd850 35#include <linux/wait.h>
e6f225eb 36#include <linux/kernel.h>
1da177e4
LT
37
38#include <net/ip.h>
39#include <net/sock.h>
1da177e4
LT
40
41#include <net/ip_vs.h>
42
43#define IP_VS_SYNC_GROUP 0xe0000051 /* multicast addr - 224.0.0.81 */
44#define IP_VS_SYNC_PORT 8848 /* multicast port */
45
46
47/*
48 * IPVS sync connection entry
49 */
50struct ip_vs_sync_conn {
51 __u8 reserved;
52
53 /* Protocol, addresses and port numbers */
54 __u8 protocol; /* Which protocol (TCP/UDP) */
014d730d
AV
55 __be16 cport;
56 __be16 vport;
57 __be16 dport;
58 __be32 caddr; /* client address */
59 __be32 vaddr; /* virtual address */
60 __be32 daddr; /* destination address */
1da177e4
LT
61
62 /* Flags and state transition */
014d730d
AV
63 __be16 flags; /* status flags */
64 __be16 state; /* state info */
1da177e4
LT
65
66 /* The sequence options start here */
67};
68
69struct ip_vs_sync_conn_options {
70 struct ip_vs_seq in_seq; /* incoming seq. struct */
71 struct ip_vs_seq out_seq; /* outgoing seq. struct */
72};
73
cc0191ae 74struct ip_vs_sync_thread_data {
998e7a76
SW
75 struct socket *sock;
76 char *buf;
cc0191ae
NH
77};
78
1da177e4
LT
79#define SIMPLE_CONN_SIZE (sizeof(struct ip_vs_sync_conn))
80#define FULL_CONN_SIZE \
81(sizeof(struct ip_vs_sync_conn) + sizeof(struct ip_vs_sync_conn_options))
82
83
84/*
85 The master mulitcasts messages to the backup load balancers in the
86 following format.
87
88 0 1 2 3
89 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
90 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91 | Count Conns | SyncID | Size |
92 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93 | |
94 | IPVS Sync Connection (1) |
95 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96 | . |
97 | . |
98 | . |
99 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100 | |
101 | IPVS Sync Connection (n) |
102 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103*/
104
105#define SYNC_MESG_HEADER_LEN 4
e6f225eb 106#define MAX_CONNS_PER_SYNCBUFF 255 /* nr_conns in ip_vs_sync_mesg is 8 bit */
1da177e4
LT
107
108struct ip_vs_sync_mesg {
109 __u8 nr_conns;
110 __u8 syncid;
111 __u16 size;
112
113 /* ip_vs_sync_conn entries start here */
114};
115
116/* the maximum length of sync (sending/receiving) message */
117static int sync_send_mesg_maxlen;
118static int sync_recv_mesg_maxlen;
119
120struct ip_vs_sync_buff {
121 struct list_head list;
122 unsigned long firstuse;
123
124 /* pointers for the message data */
125 struct ip_vs_sync_mesg *mesg;
126 unsigned char *head;
127 unsigned char *end;
128};
129
130
131/* the sync_buff list head and the lock */
132static LIST_HEAD(ip_vs_sync_queue);
133static DEFINE_SPINLOCK(ip_vs_sync_lock);
134
135/* current sync_buff for accepting new conn entries */
136static struct ip_vs_sync_buff *curr_sb = NULL;
137static DEFINE_SPINLOCK(curr_sb_lock);
138
139/* ipvs sync daemon state */
140volatile int ip_vs_sync_state = IP_VS_STATE_NONE;
141volatile int ip_vs_master_syncid = 0;
142volatile int ip_vs_backup_syncid = 0;
143
144/* multicast interface name */
145char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
146char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
147
998e7a76
SW
148/* sync daemon tasks */
149static struct task_struct *sync_master_thread;
150static struct task_struct *sync_backup_thread;
151
1da177e4 152/* multicast addr */
d5640050
SW
153static struct sockaddr_in mcast_addr = {
154 .sin_family = AF_INET,
09640e63
HH
155 .sin_port = cpu_to_be16(IP_VS_SYNC_PORT),
156 .sin_addr.s_addr = cpu_to_be32(IP_VS_SYNC_GROUP),
d5640050 157};
1da177e4
LT
158
159
998e7a76 160static inline struct ip_vs_sync_buff *sb_dequeue(void)
1da177e4
LT
161{
162 struct ip_vs_sync_buff *sb;
163
164 spin_lock_bh(&ip_vs_sync_lock);
165 if (list_empty(&ip_vs_sync_queue)) {
166 sb = NULL;
167 } else {
168 sb = list_entry(ip_vs_sync_queue.next,
169 struct ip_vs_sync_buff,
170 list);
171 list_del(&sb->list);
172 }
173 spin_unlock_bh(&ip_vs_sync_lock);
174
175 return sb;
176}
177
178static inline struct ip_vs_sync_buff * ip_vs_sync_buff_create(void)
179{
180 struct ip_vs_sync_buff *sb;
181
182 if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
183 return NULL;
184
185 if (!(sb->mesg=kmalloc(sync_send_mesg_maxlen, GFP_ATOMIC))) {
186 kfree(sb);
187 return NULL;
188 }
189 sb->mesg->nr_conns = 0;
190 sb->mesg->syncid = ip_vs_master_syncid;
191 sb->mesg->size = 4;
192 sb->head = (unsigned char *)sb->mesg + 4;
193 sb->end = (unsigned char *)sb->mesg + sync_send_mesg_maxlen;
194 sb->firstuse = jiffies;
195 return sb;
196}
197
198static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff *sb)
199{
200 kfree(sb->mesg);
201 kfree(sb);
202}
203
998e7a76
SW
204static inline void sb_queue_tail(struct ip_vs_sync_buff *sb)
205{
206 spin_lock(&ip_vs_sync_lock);
207 if (ip_vs_sync_state & IP_VS_STATE_MASTER)
208 list_add_tail(&sb->list, &ip_vs_sync_queue);
209 else
210 ip_vs_sync_buff_release(sb);
211 spin_unlock(&ip_vs_sync_lock);
212}
213
1da177e4
LT
214/*
215 * Get the current sync buffer if it has been created for more
216 * than the specified time or the specified time is zero.
217 */
218static inline struct ip_vs_sync_buff *
219get_curr_sync_buff(unsigned long time)
220{
221 struct ip_vs_sync_buff *sb;
222
223 spin_lock_bh(&curr_sb_lock);
224 if (curr_sb && (time == 0 ||
225 time_before(jiffies - curr_sb->firstuse, time))) {
226 sb = curr_sb;
227 curr_sb = NULL;
228 } else
229 sb = NULL;
230 spin_unlock_bh(&curr_sb_lock);
231 return sb;
232}
233
234
235/*
236 * Add an ip_vs_conn information into the current sync_buff.
237 * Called by ip_vs_in.
238 */
239void ip_vs_sync_conn(struct ip_vs_conn *cp)
240{
241 struct ip_vs_sync_mesg *m;
242 struct ip_vs_sync_conn *s;
243 int len;
244
245 spin_lock(&curr_sb_lock);
246 if (!curr_sb) {
247 if (!(curr_sb=ip_vs_sync_buff_create())) {
248 spin_unlock(&curr_sb_lock);
1e3e238e 249 pr_err("ip_vs_sync_buff_create failed.\n");
1da177e4
LT
250 return;
251 }
252 }
253
254 len = (cp->flags & IP_VS_CONN_F_SEQ_MASK) ? FULL_CONN_SIZE :
255 SIMPLE_CONN_SIZE;
256 m = curr_sb->mesg;
257 s = (struct ip_vs_sync_conn *)curr_sb->head;
258
259 /* copy members */
260 s->protocol = cp->protocol;
261 s->cport = cp->cport;
262 s->vport = cp->vport;
263 s->dport = cp->dport;
e7ade46a
JV
264 s->caddr = cp->caddr.ip;
265 s->vaddr = cp->vaddr.ip;
266 s->daddr = cp->daddr.ip;
1da177e4
LT
267 s->flags = htons(cp->flags & ~IP_VS_CONN_F_HASHED);
268 s->state = htons(cp->state);
269 if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
270 struct ip_vs_sync_conn_options *opt =
271 (struct ip_vs_sync_conn_options *)&s[1];
272 memcpy(opt, &cp->in_seq, sizeof(*opt));
273 }
274
275 m->nr_conns++;
276 m->size += len;
277 curr_sb->head += len;
278
279 /* check if there is a space for next one */
280 if (curr_sb->head+FULL_CONN_SIZE > curr_sb->end) {
281 sb_queue_tail(curr_sb);
282 curr_sb = NULL;
283 }
284 spin_unlock(&curr_sb_lock);
285
286 /* synchronize its controller if it has */
287 if (cp->control)
288 ip_vs_sync_conn(cp->control);
289}
290
291
292/*
293 * Process received multicast message and create the corresponding
294 * ip_vs_conn entries.
295 */
296static void ip_vs_process_message(const char *buffer, const size_t buflen)
297{
298 struct ip_vs_sync_mesg *m = (struct ip_vs_sync_mesg *)buffer;
299 struct ip_vs_sync_conn *s;
300 struct ip_vs_sync_conn_options *opt;
301 struct ip_vs_conn *cp;
5c81833c 302 struct ip_vs_protocol *pp;
1e356f9c 303 struct ip_vs_dest *dest;
f11017ec 304 struct ip_vs_conn_param param;
1da177e4
LT
305 char *p;
306 int i;
307
2ad17def
JA
308 if (buflen < sizeof(struct ip_vs_sync_mesg)) {
309 IP_VS_ERR_RL("sync message header too short\n");
310 return;
311 }
312
1da177e4
LT
313 /* Convert size back to host byte order */
314 m->size = ntohs(m->size);
315
316 if (buflen != m->size) {
2ad17def 317 IP_VS_ERR_RL("bogus sync message size\n");
1da177e4
LT
318 return;
319 }
320
321 /* SyncID sanity check */
322 if (ip_vs_backup_syncid != 0 && m->syncid != ip_vs_backup_syncid) {
323 IP_VS_DBG(7, "Ignoring incoming msg with syncid = %d\n",
324 m->syncid);
325 return;
326 }
327
328 p = (char *)buffer + sizeof(struct ip_vs_sync_mesg);
329 for (i=0; i<m->nr_conns; i++) {
b209639e 330 unsigned flags, state;
87375ab4 331
2ad17def
JA
332 if (p + SIMPLE_CONN_SIZE > buffer+buflen) {
333 IP_VS_ERR_RL("bogus conn in sync message\n");
334 return;
335 }
336 s = (struct ip_vs_sync_conn *) p;
7a4fbb1f 337 flags = ntohs(s->flags) | IP_VS_CONN_F_SYNC;
2ad17def
JA
338 flags &= ~IP_VS_CONN_F_HASHED;
339 if (flags & IP_VS_CONN_F_SEQ_MASK) {
340 opt = (struct ip_vs_sync_conn_options *)&s[1];
341 p += FULL_CONN_SIZE;
342 if (p > buffer+buflen) {
343 IP_VS_ERR_RL("bogus conn options in sync message\n");
344 return;
345 }
346 } else {
347 opt = NULL;
348 p += SIMPLE_CONN_SIZE;
349 }
350
b209639e 351 state = ntohs(s->state);
2ad17def
JA
352 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
353 pp = ip_vs_proto_get(s->protocol);
354 if (!pp) {
355 IP_VS_ERR_RL("Unsupported protocol %u in sync msg\n",
356 s->protocol);
357 continue;
358 }
359 if (state >= pp->num_states) {
360 IP_VS_DBG(2, "Invalid %s state %u in sync msg\n",
361 pp->name, state);
362 continue;
363 }
364 } else {
365 /* protocol in templates is not used for state/timeout */
366 pp = NULL;
367 if (state > 0) {
368 IP_VS_DBG(2, "Invalid template state %u in sync msg\n",
369 state);
370 state = 0;
371 }
372 }
373
f11017ec
SH
374 {
375 ip_vs_conn_fill_param(AF_INET, s->protocol,
376 (union nf_inet_addr *)&s->caddr,
377 s->cport,
378 (union nf_inet_addr *)&s->vaddr,
379 s->vport, &param);
380 if (!(flags & IP_VS_CONN_F_TEMPLATE))
381 cp = ip_vs_conn_in_get(&param);
382 else
383 cp = ip_vs_ct_in_get(&param);
384 }
1da177e4 385 if (!cp) {
1e356f9c
RB
386 /*
387 * Find the appropriate destination for the connection.
388 * If it is not found the connection will remain unbound
389 * but still handled.
390 */
7937df15
JV
391 dest = ip_vs_find_dest(AF_INET,
392 (union nf_inet_addr *)&s->daddr,
393 s->dport,
394 (union nf_inet_addr *)&s->vaddr,
395 s->vport,
1e356f9c 396 s->protocol);
b209639e
RB
397 /* Set the approprite ativity flag */
398 if (s->protocol == IPPROTO_TCP) {
399 if (state != IP_VS_TCP_S_ESTABLISHED)
400 flags |= IP_VS_CONN_F_INACTIVE;
401 else
402 flags &= ~IP_VS_CONN_F_INACTIVE;
2906f66a
VMR
403 } else if (s->protocol == IPPROTO_SCTP) {
404 if (state != IP_VS_SCTP_S_ESTABLISHED)
405 flags |= IP_VS_CONN_F_INACTIVE;
406 else
407 flags &= ~IP_VS_CONN_F_INACTIVE;
b209639e 408 }
f11017ec 409 cp = ip_vs_conn_new(&param,
cd9fe6c4 410 (union nf_inet_addr *)&s->daddr,
f11017ec 411 s->dport, flags, dest);
1e356f9c
RB
412 if (dest)
413 atomic_dec(&dest->refcnt);
1da177e4 414 if (!cp) {
1e3e238e 415 pr_err("ip_vs_conn_new failed\n");
1da177e4
LT
416 return;
417 }
1da177e4 418 } else if (!cp->dest) {
1e356f9c 419 dest = ip_vs_try_bind_dest(cp);
2ad17def 420 if (dest)
1e356f9c 421 atomic_dec(&dest->refcnt);
b209639e
RB
422 } else if ((cp->dest) && (cp->protocol == IPPROTO_TCP) &&
423 (cp->state != state)) {
424 /* update active/inactive flag for the connection */
425 dest = cp->dest;
426 if (!(cp->flags & IP_VS_CONN_F_INACTIVE) &&
427 (state != IP_VS_TCP_S_ESTABLISHED)) {
428 atomic_dec(&dest->activeconns);
429 atomic_inc(&dest->inactconns);
430 cp->flags |= IP_VS_CONN_F_INACTIVE;
431 } else if ((cp->flags & IP_VS_CONN_F_INACTIVE) &&
432 (state == IP_VS_TCP_S_ESTABLISHED)) {
433 atomic_inc(&dest->activeconns);
434 atomic_dec(&dest->inactconns);
435 cp->flags &= ~IP_VS_CONN_F_INACTIVE;
436 }
2906f66a
VMR
437 } else if ((cp->dest) && (cp->protocol == IPPROTO_SCTP) &&
438 (cp->state != state)) {
439 dest = cp->dest;
440 if (!(cp->flags & IP_VS_CONN_F_INACTIVE) &&
441 (state != IP_VS_SCTP_S_ESTABLISHED)) {
442 atomic_dec(&dest->activeconns);
443 atomic_inc(&dest->inactconns);
444 cp->flags &= ~IP_VS_CONN_F_INACTIVE;
445 }
b209639e 446 }
1da177e4 447
2ad17def 448 if (opt)
1da177e4 449 memcpy(&cp->in_seq, opt, sizeof(*opt));
1da177e4 450 atomic_set(&cp->in_pkts, sysctl_ip_vs_sync_threshold[0]);
b209639e 451 cp->state = state;
2ad17def
JA
452 cp->old_state = cp->state;
453 /*
454 * We can not recover the right timeout for templates
455 * in all cases, we can not find the right fwmark
456 * virtual service. If needed, we can do it for
457 * non-fwmark persistent services.
458 */
459 if (!(flags & IP_VS_CONN_F_TEMPLATE) && pp->timeout_table)
460 cp->timeout = pp->timeout_table[state];
461 else
462 cp->timeout = (3*60*HZ);
1da177e4 463 ip_vs_conn_put(cp);
1da177e4
LT
464 }
465}
466
467
468/*
469 * Setup loopback of outgoing multicasts on a sending socket
470 */
471static void set_mcast_loop(struct sock *sk, u_char loop)
472{
473 struct inet_sock *inet = inet_sk(sk);
474
475 /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
476 lock_sock(sk);
477 inet->mc_loop = loop ? 1 : 0;
478 release_sock(sk);
479}
480
481/*
482 * Specify TTL for outgoing multicasts on a sending socket
483 */
484static void set_mcast_ttl(struct sock *sk, u_char ttl)
485{
486 struct inet_sock *inet = inet_sk(sk);
487
488 /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
489 lock_sock(sk);
490 inet->mc_ttl = ttl;
491 release_sock(sk);
492}
493
494/*
495 * Specifiy default interface for outgoing multicasts
496 */
497static int set_mcast_if(struct sock *sk, char *ifname)
498{
499 struct net_device *dev;
500 struct inet_sock *inet = inet_sk(sk);
501
881d966b 502 if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
1da177e4
LT
503 return -ENODEV;
504
505 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
506 return -EINVAL;
507
508 lock_sock(sk);
509 inet->mc_index = dev->ifindex;
510 /* inet->mc_addr = 0; */
511 release_sock(sk);
512
513 return 0;
514}
515
516
517/*
518 * Set the maximum length of sync message according to the
519 * specified interface's MTU.
520 */
521static int set_sync_mesg_maxlen(int sync_state)
522{
523 struct net_device *dev;
524 int num;
525
526 if (sync_state == IP_VS_STATE_MASTER) {
881d966b 527 if ((dev = __dev_get_by_name(&init_net, ip_vs_master_mcast_ifn)) == NULL)
1da177e4
LT
528 return -ENODEV;
529
530 num = (dev->mtu - sizeof(struct iphdr) -
531 sizeof(struct udphdr) -
532 SYNC_MESG_HEADER_LEN - 20) / SIMPLE_CONN_SIZE;
e6f225eb
SW
533 sync_send_mesg_maxlen = SYNC_MESG_HEADER_LEN +
534 SIMPLE_CONN_SIZE * min(num, MAX_CONNS_PER_SYNCBUFF);
1da177e4
LT
535 IP_VS_DBG(7, "setting the maximum length of sync sending "
536 "message %d.\n", sync_send_mesg_maxlen);
537 } else if (sync_state == IP_VS_STATE_BACKUP) {
881d966b 538 if ((dev = __dev_get_by_name(&init_net, ip_vs_backup_mcast_ifn)) == NULL)
1da177e4
LT
539 return -ENODEV;
540
541 sync_recv_mesg_maxlen = dev->mtu -
542 sizeof(struct iphdr) - sizeof(struct udphdr);
543 IP_VS_DBG(7, "setting the maximum length of sync receiving "
544 "message %d.\n", sync_recv_mesg_maxlen);
545 }
546
547 return 0;
548}
549
550
551/*
552 * Join a multicast group.
553 * the group is specified by a class D multicast address 224.0.0.0/8
554 * in the in_addr structure passed in as a parameter.
555 */
556static int
557join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
558{
559 struct ip_mreqn mreq;
560 struct net_device *dev;
561 int ret;
562
563 memset(&mreq, 0, sizeof(mreq));
564 memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
565
881d966b 566 if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
1da177e4
LT
567 return -ENODEV;
568 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
569 return -EINVAL;
570
571 mreq.imr_ifindex = dev->ifindex;
572
573 lock_sock(sk);
574 ret = ip_mc_join_group(sk, &mreq);
575 release_sock(sk);
576
577 return ret;
578}
579
580
581static int bind_mcastif_addr(struct socket *sock, char *ifname)
582{
583 struct net_device *dev;
a61ced5d 584 __be32 addr;
1da177e4
LT
585 struct sockaddr_in sin;
586
881d966b 587 if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
1da177e4
LT
588 return -ENODEV;
589
590 addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
591 if (!addr)
1e3e238e
HE
592 pr_err("You probably need to specify IP address on "
593 "multicast interface.\n");
1da177e4 594
14d5e834
HH
595 IP_VS_DBG(7, "binding socket with (%s) %pI4\n",
596 ifname, &addr);
1da177e4
LT
597
598 /* Now bind the socket with the address of multicast interface */
599 sin.sin_family = AF_INET;
600 sin.sin_addr.s_addr = addr;
601 sin.sin_port = 0;
602
603 return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
604}
605
606/*
607 * Set up sending multicast socket over UDP
608 */
609static struct socket * make_send_sock(void)
610{
611 struct socket *sock;
e6dd731c 612 int result;
1da177e4
LT
613
614 /* First create a socket */
e6dd731c
SW
615 result = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
616 if (result < 0) {
1e3e238e 617 pr_err("Error during creation of socket; terminating\n");
e6dd731c 618 return ERR_PTR(result);
1da177e4
LT
619 }
620
e6dd731c
SW
621 result = set_mcast_if(sock->sk, ip_vs_master_mcast_ifn);
622 if (result < 0) {
1e3e238e 623 pr_err("Error setting outbound mcast interface\n");
1da177e4
LT
624 goto error;
625 }
626
627 set_mcast_loop(sock->sk, 0);
628 set_mcast_ttl(sock->sk, 1);
629
e6dd731c
SW
630 result = bind_mcastif_addr(sock, ip_vs_master_mcast_ifn);
631 if (result < 0) {
1e3e238e 632 pr_err("Error binding address of the mcast interface\n");
1da177e4
LT
633 goto error;
634 }
635
e6dd731c
SW
636 result = sock->ops->connect(sock, (struct sockaddr *) &mcast_addr,
637 sizeof(struct sockaddr), 0);
638 if (result < 0) {
1e3e238e 639 pr_err("Error connecting to the multicast addr\n");
1da177e4
LT
640 goto error;
641 }
642
643 return sock;
644
645 error:
646 sock_release(sock);
e6dd731c 647 return ERR_PTR(result);
1da177e4
LT
648}
649
650
651/*
652 * Set up receiving multicast socket over UDP
653 */
654static struct socket * make_receive_sock(void)
655{
656 struct socket *sock;
e6dd731c 657 int result;
1da177e4
LT
658
659 /* First create a socket */
e6dd731c
SW
660 result = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
661 if (result < 0) {
1e3e238e 662 pr_err("Error during creation of socket; terminating\n");
e6dd731c 663 return ERR_PTR(result);
1da177e4
LT
664 }
665
666 /* it is equivalent to the REUSEADDR option in user-space */
667 sock->sk->sk_reuse = 1;
668
e6dd731c
SW
669 result = sock->ops->bind(sock, (struct sockaddr *) &mcast_addr,
670 sizeof(struct sockaddr));
671 if (result < 0) {
1e3e238e 672 pr_err("Error binding to the multicast addr\n");
1da177e4
LT
673 goto error;
674 }
675
676 /* join the multicast group */
e6dd731c
SW
677 result = join_mcast_group(sock->sk,
678 (struct in_addr *) &mcast_addr.sin_addr,
679 ip_vs_backup_mcast_ifn);
680 if (result < 0) {
1e3e238e 681 pr_err("Error joining to the multicast group\n");
1da177e4
LT
682 goto error;
683 }
684
685 return sock;
686
687 error:
688 sock_release(sock);
e6dd731c 689 return ERR_PTR(result);
1da177e4
LT
690}
691
692
693static int
694ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
695{
696 struct msghdr msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
697 struct kvec iov;
698 int len;
699
700 EnterFunction(7);
701 iov.iov_base = (void *)buffer;
702 iov.iov_len = length;
703
704 len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
705
706 LeaveFunction(7);
707 return len;
708}
709
710static void
711ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
712{
713 int msize;
714
715 msize = msg->size;
716
717 /* Put size in network byte order */
718 msg->size = htons(msg->size);
719
720 if (ip_vs_send_async(sock, (char *)msg, msize) != msize)
1e3e238e 721 pr_err("ip_vs_send_async error\n");
1da177e4
LT
722}
723
724static int
725ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
726{
727 struct msghdr msg = {NULL,};
728 struct kvec iov;
729 int len;
730
731 EnterFunction(7);
732
733 /* Receive a packet */
734 iov.iov_base = buffer;
735 iov.iov_len = (size_t)buflen;
736
737 len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, 0);
738
739 if (len < 0)
740 return -1;
741
742 LeaveFunction(7);
743 return len;
744}
745
746
998e7a76 747static int sync_thread_master(void *data)
1da177e4 748{
998e7a76 749 struct ip_vs_sync_thread_data *tinfo = data;
1da177e4
LT
750 struct ip_vs_sync_buff *sb;
751
1e3e238e
HE
752 pr_info("sync thread started: state = MASTER, mcast_ifn = %s, "
753 "syncid = %d\n",
754 ip_vs_master_mcast_ifn, ip_vs_master_syncid);
1da177e4 755
998e7a76
SW
756 while (!kthread_should_stop()) {
757 while ((sb = sb_dequeue())) {
758 ip_vs_send_sync_msg(tinfo->sock, sb->mesg);
1da177e4
LT
759 ip_vs_sync_buff_release(sb);
760 }
761
762 /* check if entries stay in curr_sb for 2 seconds */
998e7a76
SW
763 sb = get_curr_sync_buff(2 * HZ);
764 if (sb) {
765 ip_vs_send_sync_msg(tinfo->sock, sb->mesg);
1da177e4
LT
766 ip_vs_sync_buff_release(sb);
767 }
768
375c6bba 769 schedule_timeout_interruptible(HZ);
1da177e4
LT
770 }
771
772 /* clean up the sync_buff queue */
773 while ((sb=sb_dequeue())) {
774 ip_vs_sync_buff_release(sb);
775 }
776
777 /* clean up the current sync_buff */
778 if ((sb = get_curr_sync_buff(0))) {
779 ip_vs_sync_buff_release(sb);
780 }
781
782 /* release the sending multicast socket */
998e7a76
SW
783 sock_release(tinfo->sock);
784 kfree(tinfo);
785
786 return 0;
1da177e4
LT
787}
788
789
998e7a76 790static int sync_thread_backup(void *data)
1da177e4 791{
998e7a76 792 struct ip_vs_sync_thread_data *tinfo = data;
1da177e4
LT
793 int len;
794
1e3e238e
HE
795 pr_info("sync thread started: state = BACKUP, mcast_ifn = %s, "
796 "syncid = %d\n",
797 ip_vs_backup_mcast_ifn, ip_vs_backup_syncid);
1da177e4 798
998e7a76 799 while (!kthread_should_stop()) {
aa395145 800 wait_event_interruptible(*sk_sleep(tinfo->sock->sk),
ba6fd850
SW
801 !skb_queue_empty(&tinfo->sock->sk->sk_receive_queue)
802 || kthread_should_stop());
803
998e7a76
SW
804 /* do we have data now? */
805 while (!skb_queue_empty(&(tinfo->sock->sk->sk_receive_queue))) {
806 len = ip_vs_receive(tinfo->sock, tinfo->buf,
807 sync_recv_mesg_maxlen);
808 if (len <= 0) {
1e3e238e 809 pr_err("receiving message error\n");
1da177e4
LT
810 break;
811 }
998e7a76
SW
812
813 /* disable bottom half, because it accesses the data
1da177e4
LT
814 shared by softirq while getting/creating conns */
815 local_bh_disable();
998e7a76 816 ip_vs_process_message(tinfo->buf, len);
1da177e4
LT
817 local_bh_enable();
818 }
1da177e4
LT
819 }
820
821 /* release the sending multicast socket */
998e7a76
SW
822 sock_release(tinfo->sock);
823 kfree(tinfo->buf);
824 kfree(tinfo);
1da177e4 825
998e7a76 826 return 0;
1da177e4
LT
827}
828
829
998e7a76 830int start_sync_thread(int state, char *mcast_ifn, __u8 syncid)
1da177e4 831{
998e7a76
SW
832 struct ip_vs_sync_thread_data *tinfo;
833 struct task_struct **realtask, *task;
834 struct socket *sock;
835 char *name, *buf = NULL;
836 int (*threadfn)(void *data);
837 int result = -ENOMEM;
1da177e4 838
1e3e238e 839 IP_VS_DBG(7, "%s(): pid %d\n", __func__, task_pid_nr(current));
998e7a76
SW
840 IP_VS_DBG(7, "Each ip_vs_sync_conn entry needs %Zd bytes\n",
841 sizeof(struct ip_vs_sync_conn));
1da177e4 842
998e7a76
SW
843 if (state == IP_VS_STATE_MASTER) {
844 if (sync_master_thread)
845 return -EEXIST;
1da177e4 846
998e7a76
SW
847 strlcpy(ip_vs_master_mcast_ifn, mcast_ifn,
848 sizeof(ip_vs_master_mcast_ifn));
849 ip_vs_master_syncid = syncid;
850 realtask = &sync_master_thread;
1da177e4 851 name = "ipvs_syncmaster";
998e7a76
SW
852 threadfn = sync_thread_master;
853 sock = make_send_sock();
854 } else if (state == IP_VS_STATE_BACKUP) {
855 if (sync_backup_thread)
856 return -EEXIST;
857
858 strlcpy(ip_vs_backup_mcast_ifn, mcast_ifn,
859 sizeof(ip_vs_backup_mcast_ifn));
860 ip_vs_backup_syncid = syncid;
861 realtask = &sync_backup_thread;
1da177e4 862 name = "ipvs_syncbackup";
998e7a76
SW
863 threadfn = sync_thread_backup;
864 sock = make_receive_sock();
1da177e4 865 } else {
1da177e4
LT
866 return -EINVAL;
867 }
868
998e7a76
SW
869 if (IS_ERR(sock)) {
870 result = PTR_ERR(sock);
871 goto out;
872 }
1da177e4 873
1da177e4 874 set_sync_mesg_maxlen(state);
998e7a76
SW
875 if (state == IP_VS_STATE_BACKUP) {
876 buf = kmalloc(sync_recv_mesg_maxlen, GFP_KERNEL);
877 if (!buf)
878 goto outsocket;
879 }
1da177e4 880
998e7a76
SW
881 tinfo = kmalloc(sizeof(*tinfo), GFP_KERNEL);
882 if (!tinfo)
883 goto outbuf;
cc0191ae 884
998e7a76
SW
885 tinfo->sock = sock;
886 tinfo->buf = buf;
1da177e4 887
998e7a76
SW
888 task = kthread_run(threadfn, tinfo, name);
889 if (IS_ERR(task)) {
890 result = PTR_ERR(task);
891 goto outtinfo;
892 }
1da177e4 893
998e7a76
SW
894 /* mark as active */
895 *realtask = task;
896 ip_vs_sync_state |= state;
1da177e4 897
998e7a76
SW
898 /* increase the module use count */
899 ip_vs_use_count_inc();
1da177e4
LT
900
901 return 0;
1da177e4 902
998e7a76
SW
903outtinfo:
904 kfree(tinfo);
905outbuf:
906 kfree(buf);
907outsocket:
908 sock_release(sock);
909out:
910 return result;
1da177e4
LT
911}
912
913
998e7a76 914int stop_sync_thread(int state)
1da177e4 915{
1e3e238e 916 IP_VS_DBG(7, "%s(): pid %d\n", __func__, task_pid_nr(current));
1da177e4 917
1da177e4 918 if (state == IP_VS_STATE_MASTER) {
998e7a76
SW
919 if (!sync_master_thread)
920 return -ESRCH;
cc0191ae 921
1e3e238e
HE
922 pr_info("stopping master sync thread %d ...\n",
923 task_pid_nr(sync_master_thread));
1da177e4 924
998e7a76
SW
925 /*
926 * The lock synchronizes with sb_queue_tail(), so that we don't
927 * add sync buffers to the queue, when we are already in
928 * progress of stopping the master sync daemon.
929 */
1da177e4 930
bc0fde2f 931 spin_lock_bh(&ip_vs_sync_lock);
998e7a76 932 ip_vs_sync_state &= ~IP_VS_STATE_MASTER;
bc0fde2f 933 spin_unlock_bh(&ip_vs_sync_lock);
998e7a76
SW
934 kthread_stop(sync_master_thread);
935 sync_master_thread = NULL;
936 } else if (state == IP_VS_STATE_BACKUP) {
937 if (!sync_backup_thread)
938 return -ESRCH;
939
1e3e238e
HE
940 pr_info("stopping backup sync thread %d ...\n",
941 task_pid_nr(sync_backup_thread));
998e7a76
SW
942
943 ip_vs_sync_state &= ~IP_VS_STATE_BACKUP;
944 kthread_stop(sync_backup_thread);
945 sync_backup_thread = NULL;
946 } else {
947 return -EINVAL;
948 }
1da177e4 949
998e7a76
SW
950 /* decrease the module use count */
951 ip_vs_use_count_dec();
1da177e4
LT
952
953 return 0;
954}