blob: 3ce1093e067056d61aa9b1b9adfdada87f68bfa4 [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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
20#include <linux/module.h>
21#include <linux/slab.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020022#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/net.h>
24#include <linux/completion.h>
25#include <linux/delay.h>
26#include <linux/skbuff.h>
27#include <linux/in.h>
28#include <linux/igmp.h> /* for ip_mc_join_group */
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020029#include <linux/udp.h>
Sven Wegenere6dd7312008-07-16 11:13:43 +000030#include <linux/err.h>
Sven Wegener998e7a72008-07-16 11:13:50 +000031#include <linux/kthread.h>
Sven Wegenerba6fd852008-07-16 11:13:56 +000032#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <net/ip.h>
35#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
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 */
46struct ip_vs_sync_conn {
47 __u8 reserved;
48
49 /* Protocol, addresses and port numbers */
50 __u8 protocol; /* Which protocol (TCP/UDP) */
Al Viro014d7302006-09-28 14:29:52 -070051 __be16 cport;
52 __be16 vport;
53 __be16 dport;
54 __be32 caddr; /* client address */
55 __be32 vaddr; /* virtual address */
56 __be32 daddr; /* destination address */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 /* Flags and state transition */
Al Viro014d7302006-09-28 14:29:52 -070059 __be16 flags; /* status flags */
60 __be16 state; /* state info */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 /* The sequence options start here */
63};
64
65struct 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
Neil Hormancc0191a2007-06-18 22:33:20 -070070struct ip_vs_sync_thread_data {
Sven Wegener998e7a72008-07-16 11:13:50 +000071 struct socket *sock;
72 char *buf;
Neil Hormancc0191a2007-06-18 22:33:20 -070073};
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#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
103struct 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 */
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 /* 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 */
127static LIST_HEAD(ip_vs_sync_queue);
128static DEFINE_SPINLOCK(ip_vs_sync_lock);
129
130/* current sync_buff for accepting new conn entries */
131static struct ip_vs_sync_buff *curr_sb = NULL;
132static DEFINE_SPINLOCK(curr_sb_lock);
133
134/* ipvs sync daemon state */
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/* multicast interface name */
140char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
141char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
142
Sven Wegener998e7a72008-07-16 11:13:50 +0000143/* sync daemon tasks */
144static struct task_struct *sync_master_thread;
145static struct task_struct *sync_backup_thread;
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/* multicast addr */
Sven Wegenerd5640052008-07-16 11:13:35 +0000148static struct sockaddr_in mcast_addr = {
149 .sin_family = AF_INET,
150 .sin_port = __constant_htons(IP_VS_SYNC_PORT),
151 .sin_addr.s_addr = __constant_htonl(IP_VS_SYNC_GROUP),
152};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154
Sven Wegener998e7a72008-07-16 11:13:50 +0000155static inline struct ip_vs_sync_buff *sb_dequeue(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 struct ip_vs_sync_buff *sb;
158
159 spin_lock_bh(&ip_vs_sync_lock);
160 if (list_empty(&ip_vs_sync_queue)) {
161 sb = NULL;
162 } else {
163 sb = list_entry(ip_vs_sync_queue.next,
164 struct ip_vs_sync_buff,
165 list);
166 list_del(&sb->list);
167 }
168 spin_unlock_bh(&ip_vs_sync_lock);
169
170 return sb;
171}
172
173static inline struct ip_vs_sync_buff * ip_vs_sync_buff_create(void)
174{
175 struct ip_vs_sync_buff *sb;
176
177 if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
178 return NULL;
179
180 if (!(sb->mesg=kmalloc(sync_send_mesg_maxlen, GFP_ATOMIC))) {
181 kfree(sb);
182 return NULL;
183 }
184 sb->mesg->nr_conns = 0;
185 sb->mesg->syncid = ip_vs_master_syncid;
186 sb->mesg->size = 4;
187 sb->head = (unsigned char *)sb->mesg + 4;
188 sb->end = (unsigned char *)sb->mesg + sync_send_mesg_maxlen;
189 sb->firstuse = jiffies;
190 return sb;
191}
192
193static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff *sb)
194{
195 kfree(sb->mesg);
196 kfree(sb);
197}
198
Sven Wegener998e7a72008-07-16 11:13:50 +0000199static inline void sb_queue_tail(struct ip_vs_sync_buff *sb)
200{
201 spin_lock(&ip_vs_sync_lock);
202 if (ip_vs_sync_state & IP_VS_STATE_MASTER)
203 list_add_tail(&sb->list, &ip_vs_sync_queue);
204 else
205 ip_vs_sync_buff_release(sb);
206 spin_unlock(&ip_vs_sync_lock);
207}
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
210 * Get the current sync buffer if it has been created for more
211 * than the specified time or the specified time is zero.
212 */
213static inline struct ip_vs_sync_buff *
214get_curr_sync_buff(unsigned long time)
215{
216 struct ip_vs_sync_buff *sb;
217
218 spin_lock_bh(&curr_sb_lock);
219 if (curr_sb && (time == 0 ||
220 time_before(jiffies - curr_sb->firstuse, time))) {
221 sb = curr_sb;
222 curr_sb = NULL;
223 } else
224 sb = NULL;
225 spin_unlock_bh(&curr_sb_lock);
226 return sb;
227}
228
229
230/*
231 * Add an ip_vs_conn information into the current sync_buff.
232 * Called by ip_vs_in.
233 */
234void ip_vs_sync_conn(struct ip_vs_conn *cp)
235{
236 struct ip_vs_sync_mesg *m;
237 struct ip_vs_sync_conn *s;
238 int len;
239
240 spin_lock(&curr_sb_lock);
241 if (!curr_sb) {
242 if (!(curr_sb=ip_vs_sync_buff_create())) {
243 spin_unlock(&curr_sb_lock);
244 IP_VS_ERR("ip_vs_sync_buff_create failed.\n");
245 return;
246 }
247 }
248
249 len = (cp->flags & IP_VS_CONN_F_SEQ_MASK) ? FULL_CONN_SIZE :
250 SIMPLE_CONN_SIZE;
251 m = curr_sb->mesg;
252 s = (struct ip_vs_sync_conn *)curr_sb->head;
253
254 /* copy members */
255 s->protocol = cp->protocol;
256 s->cport = cp->cport;
257 s->vport = cp->vport;
258 s->dport = cp->dport;
Julius Volze7ade462008-09-02 15:55:33 +0200259 s->caddr = cp->caddr.ip;
260 s->vaddr = cp->vaddr.ip;
261 s->daddr = cp->daddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 s->flags = htons(cp->flags & ~IP_VS_CONN_F_HASHED);
263 s->state = htons(cp->state);
264 if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
265 struct ip_vs_sync_conn_options *opt =
266 (struct ip_vs_sync_conn_options *)&s[1];
267 memcpy(opt, &cp->in_seq, sizeof(*opt));
268 }
269
270 m->nr_conns++;
271 m->size += len;
272 curr_sb->head += len;
273
274 /* check if there is a space for next one */
275 if (curr_sb->head+FULL_CONN_SIZE > curr_sb->end) {
276 sb_queue_tail(curr_sb);
277 curr_sb = NULL;
278 }
279 spin_unlock(&curr_sb_lock);
280
281 /* synchronize its controller if it has */
282 if (cp->control)
283 ip_vs_sync_conn(cp->control);
284}
285
286
287/*
288 * Process received multicast message and create the corresponding
289 * ip_vs_conn entries.
290 */
291static void ip_vs_process_message(const char *buffer, const size_t buflen)
292{
293 struct ip_vs_sync_mesg *m = (struct ip_vs_sync_mesg *)buffer;
294 struct ip_vs_sync_conn *s;
295 struct ip_vs_sync_conn_options *opt;
296 struct ip_vs_conn *cp;
Andy Gospodarek5c818332007-10-29 04:35:45 -0700297 struct ip_vs_protocol *pp;
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800298 struct ip_vs_dest *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 char *p;
300 int i;
301
Julian Anastasov2ad17de2008-04-29 03:21:23 -0700302 if (buflen < sizeof(struct ip_vs_sync_mesg)) {
303 IP_VS_ERR_RL("sync message header too short\n");
304 return;
305 }
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 /* Convert size back to host byte order */
308 m->size = ntohs(m->size);
309
310 if (buflen != m->size) {
Julian Anastasov2ad17de2008-04-29 03:21:23 -0700311 IP_VS_ERR_RL("bogus sync message size\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return;
313 }
314
315 /* SyncID sanity check */
316 if (ip_vs_backup_syncid != 0 && m->syncid != ip_vs_backup_syncid) {
317 IP_VS_DBG(7, "Ignoring incoming msg with syncid = %d\n",
318 m->syncid);
319 return;
320 }
321
322 p = (char *)buffer + sizeof(struct ip_vs_sync_mesg);
323 for (i=0; i<m->nr_conns; i++) {
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800324 unsigned flags, state;
Julian Anastasov87375ab2005-09-14 21:08:51 -0700325
Julian Anastasov2ad17de2008-04-29 03:21:23 -0700326 if (p + SIMPLE_CONN_SIZE > buffer+buflen) {
327 IP_VS_ERR_RL("bogus conn in sync message\n");
328 return;
329 }
330 s = (struct ip_vs_sync_conn *) p;
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -0800331 flags = ntohs(s->flags) | IP_VS_CONN_F_SYNC;
Julian Anastasov2ad17de2008-04-29 03:21:23 -0700332 flags &= ~IP_VS_CONN_F_HASHED;
333 if (flags & IP_VS_CONN_F_SEQ_MASK) {
334 opt = (struct ip_vs_sync_conn_options *)&s[1];
335 p += FULL_CONN_SIZE;
336 if (p > buffer+buflen) {
337 IP_VS_ERR_RL("bogus conn options in sync message\n");
338 return;
339 }
340 } else {
341 opt = NULL;
342 p += SIMPLE_CONN_SIZE;
343 }
344
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800345 state = ntohs(s->state);
Julian Anastasov2ad17de2008-04-29 03:21:23 -0700346 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
347 pp = ip_vs_proto_get(s->protocol);
348 if (!pp) {
349 IP_VS_ERR_RL("Unsupported protocol %u in sync msg\n",
350 s->protocol);
351 continue;
352 }
353 if (state >= pp->num_states) {
354 IP_VS_DBG(2, "Invalid %s state %u in sync msg\n",
355 pp->name, state);
356 continue;
357 }
358 } else {
359 /* protocol in templates is not used for state/timeout */
360 pp = NULL;
361 if (state > 0) {
362 IP_VS_DBG(2, "Invalid template state %u in sync msg\n",
363 state);
364 state = 0;
365 }
366 }
367
Julian Anastasov87375ab2005-09-14 21:08:51 -0700368 if (!(flags & IP_VS_CONN_F_TEMPLATE))
Julius Volz28364a52008-09-02 15:55:43 +0200369 cp = ip_vs_conn_in_get(AF_INET, s->protocol,
370 (union nf_inet_addr *)&s->caddr,
371 s->cport,
372 (union nf_inet_addr *)&s->vaddr,
373 s->vport);
Julian Anastasov87375ab2005-09-14 21:08:51 -0700374 else
Julius Volz28364a52008-09-02 15:55:43 +0200375 cp = ip_vs_ct_in_get(AF_INET, s->protocol,
376 (union nf_inet_addr *)&s->caddr,
377 s->cport,
378 (union nf_inet_addr *)&s->vaddr,
379 s->vport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (!cp) {
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800381 /*
382 * Find the appropriate destination for the connection.
383 * If it is not found the connection will remain unbound
384 * but still handled.
385 */
386 dest = ip_vs_find_dest(s->daddr, s->dport,
387 s->vaddr, s->vport,
388 s->protocol);
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800389 /* Set the approprite ativity flag */
390 if (s->protocol == IPPROTO_TCP) {
391 if (state != IP_VS_TCP_S_ESTABLISHED)
392 flags |= IP_VS_CONN_F_INACTIVE;
393 else
394 flags &= ~IP_VS_CONN_F_INACTIVE;
395 }
Julius Volz28364a52008-09-02 15:55:43 +0200396 cp = ip_vs_conn_new(AF_INET, s->protocol,
397 (union nf_inet_addr *)s->caddr,
398 s->cport,
399 (union nf_inet_addr *)s->vaddr,
400 s->vport,
401 (union nf_inet_addr *)s->daddr,
402 s->dport,
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800403 flags, dest);
404 if (dest)
405 atomic_dec(&dest->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (!cp) {
407 IP_VS_ERR("ip_vs_conn_new failed\n");
408 return;
409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 } else if (!cp->dest) {
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800411 dest = ip_vs_try_bind_dest(cp);
Julian Anastasov2ad17de2008-04-29 03:21:23 -0700412 if (dest)
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800413 atomic_dec(&dest->refcnt);
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800414 } else if ((cp->dest) && (cp->protocol == IPPROTO_TCP) &&
415 (cp->state != state)) {
416 /* update active/inactive flag for the connection */
417 dest = cp->dest;
418 if (!(cp->flags & IP_VS_CONN_F_INACTIVE) &&
419 (state != IP_VS_TCP_S_ESTABLISHED)) {
420 atomic_dec(&dest->activeconns);
421 atomic_inc(&dest->inactconns);
422 cp->flags |= IP_VS_CONN_F_INACTIVE;
423 } else if ((cp->flags & IP_VS_CONN_F_INACTIVE) &&
424 (state == IP_VS_TCP_S_ESTABLISHED)) {
425 atomic_inc(&dest->activeconns);
426 atomic_dec(&dest->inactconns);
427 cp->flags &= ~IP_VS_CONN_F_INACTIVE;
428 }
429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Julian Anastasov2ad17de2008-04-29 03:21:23 -0700431 if (opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 memcpy(&cp->in_seq, opt, sizeof(*opt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 atomic_set(&cp->in_pkts, sysctl_ip_vs_sync_threshold[0]);
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800434 cp->state = state;
Julian Anastasov2ad17de2008-04-29 03:21:23 -0700435 cp->old_state = cp->state;
436 /*
437 * We can not recover the right timeout for templates
438 * in all cases, we can not find the right fwmark
439 * virtual service. If needed, we can do it for
440 * non-fwmark persistent services.
441 */
442 if (!(flags & IP_VS_CONN_F_TEMPLATE) && pp->timeout_table)
443 cp->timeout = pp->timeout_table[state];
444 else
445 cp->timeout = (3*60*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448}
449
450
451/*
452 * Setup loopback of outgoing multicasts on a sending socket
453 */
454static void set_mcast_loop(struct sock *sk, u_char loop)
455{
456 struct inet_sock *inet = inet_sk(sk);
457
458 /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
459 lock_sock(sk);
460 inet->mc_loop = loop ? 1 : 0;
461 release_sock(sk);
462}
463
464/*
465 * Specify TTL for outgoing multicasts on a sending socket
466 */
467static void set_mcast_ttl(struct sock *sk, u_char ttl)
468{
469 struct inet_sock *inet = inet_sk(sk);
470
471 /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
472 lock_sock(sk);
473 inet->mc_ttl = ttl;
474 release_sock(sk);
475}
476
477/*
478 * Specifiy default interface for outgoing multicasts
479 */
480static int set_mcast_if(struct sock *sk, char *ifname)
481{
482 struct net_device *dev;
483 struct inet_sock *inet = inet_sk(sk);
484
Eric W. Biederman881d9662007-09-17 11:56:21 -0700485 if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return -ENODEV;
487
488 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
489 return -EINVAL;
490
491 lock_sock(sk);
492 inet->mc_index = dev->ifindex;
493 /* inet->mc_addr = 0; */
494 release_sock(sk);
495
496 return 0;
497}
498
499
500/*
501 * Set the maximum length of sync message according to the
502 * specified interface's MTU.
503 */
504static int set_sync_mesg_maxlen(int sync_state)
505{
506 struct net_device *dev;
507 int num;
508
509 if (sync_state == IP_VS_STATE_MASTER) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700510 if ((dev = __dev_get_by_name(&init_net, ip_vs_master_mcast_ifn)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return -ENODEV;
512
513 num = (dev->mtu - sizeof(struct iphdr) -
514 sizeof(struct udphdr) -
515 SYNC_MESG_HEADER_LEN - 20) / SIMPLE_CONN_SIZE;
516 sync_send_mesg_maxlen =
517 SYNC_MESG_HEADER_LEN + SIMPLE_CONN_SIZE * num;
518 IP_VS_DBG(7, "setting the maximum length of sync sending "
519 "message %d.\n", sync_send_mesg_maxlen);
520 } else if (sync_state == IP_VS_STATE_BACKUP) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700521 if ((dev = __dev_get_by_name(&init_net, ip_vs_backup_mcast_ifn)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return -ENODEV;
523
524 sync_recv_mesg_maxlen = dev->mtu -
525 sizeof(struct iphdr) - sizeof(struct udphdr);
526 IP_VS_DBG(7, "setting the maximum length of sync receiving "
527 "message %d.\n", sync_recv_mesg_maxlen);
528 }
529
530 return 0;
531}
532
533
534/*
535 * Join a multicast group.
536 * the group is specified by a class D multicast address 224.0.0.0/8
537 * in the in_addr structure passed in as a parameter.
538 */
539static int
540join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
541{
542 struct ip_mreqn mreq;
543 struct net_device *dev;
544 int ret;
545
546 memset(&mreq, 0, sizeof(mreq));
547 memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
548
Eric W. Biederman881d9662007-09-17 11:56:21 -0700549 if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return -ENODEV;
551 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
552 return -EINVAL;
553
554 mreq.imr_ifindex = dev->ifindex;
555
556 lock_sock(sk);
557 ret = ip_mc_join_group(sk, &mreq);
558 release_sock(sk);
559
560 return ret;
561}
562
563
564static int bind_mcastif_addr(struct socket *sock, char *ifname)
565{
566 struct net_device *dev;
Al Viroa61ced52006-09-26 21:27:54 -0700567 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 struct sockaddr_in sin;
569
Eric W. Biederman881d9662007-09-17 11:56:21 -0700570 if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return -ENODEV;
572
573 addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
574 if (!addr)
575 IP_VS_ERR("You probably need to specify IP address on "
576 "multicast interface.\n");
577
578 IP_VS_DBG(7, "binding socket with (%s) %u.%u.%u.%u\n",
579 ifname, NIPQUAD(addr));
580
581 /* Now bind the socket with the address of multicast interface */
582 sin.sin_family = AF_INET;
583 sin.sin_addr.s_addr = addr;
584 sin.sin_port = 0;
585
586 return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
587}
588
589/*
590 * Set up sending multicast socket over UDP
591 */
592static struct socket * make_send_sock(void)
593{
594 struct socket *sock;
Sven Wegenere6dd7312008-07-16 11:13:43 +0000595 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 /* First create a socket */
Sven Wegenere6dd7312008-07-16 11:13:43 +0000598 result = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
599 if (result < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 IP_VS_ERR("Error during creation of socket; terminating\n");
Sven Wegenere6dd7312008-07-16 11:13:43 +0000601 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603
Sven Wegenere6dd7312008-07-16 11:13:43 +0000604 result = set_mcast_if(sock->sk, ip_vs_master_mcast_ifn);
605 if (result < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 IP_VS_ERR("Error setting outbound mcast interface\n");
607 goto error;
608 }
609
610 set_mcast_loop(sock->sk, 0);
611 set_mcast_ttl(sock->sk, 1);
612
Sven Wegenere6dd7312008-07-16 11:13:43 +0000613 result = bind_mcastif_addr(sock, ip_vs_master_mcast_ifn);
614 if (result < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 IP_VS_ERR("Error binding address of the mcast interface\n");
616 goto error;
617 }
618
Sven Wegenere6dd7312008-07-16 11:13:43 +0000619 result = sock->ops->connect(sock, (struct sockaddr *) &mcast_addr,
620 sizeof(struct sockaddr), 0);
621 if (result < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 IP_VS_ERR("Error connecting to the multicast addr\n");
623 goto error;
624 }
625
626 return sock;
627
628 error:
629 sock_release(sock);
Sven Wegenere6dd7312008-07-16 11:13:43 +0000630 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633
634/*
635 * Set up receiving multicast socket over UDP
636 */
637static struct socket * make_receive_sock(void)
638{
639 struct socket *sock;
Sven Wegenere6dd7312008-07-16 11:13:43 +0000640 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 /* First create a socket */
Sven Wegenere6dd7312008-07-16 11:13:43 +0000643 result = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
644 if (result < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 IP_VS_ERR("Error during creation of socket; terminating\n");
Sven Wegenere6dd7312008-07-16 11:13:43 +0000646 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648
649 /* it is equivalent to the REUSEADDR option in user-space */
650 sock->sk->sk_reuse = 1;
651
Sven Wegenere6dd7312008-07-16 11:13:43 +0000652 result = sock->ops->bind(sock, (struct sockaddr *) &mcast_addr,
653 sizeof(struct sockaddr));
654 if (result < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 IP_VS_ERR("Error binding to the multicast addr\n");
656 goto error;
657 }
658
659 /* join the multicast group */
Sven Wegenere6dd7312008-07-16 11:13:43 +0000660 result = join_mcast_group(sock->sk,
661 (struct in_addr *) &mcast_addr.sin_addr,
662 ip_vs_backup_mcast_ifn);
663 if (result < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 IP_VS_ERR("Error joining to the multicast group\n");
665 goto error;
666 }
667
668 return sock;
669
670 error:
671 sock_release(sock);
Sven Wegenere6dd7312008-07-16 11:13:43 +0000672 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675
676static int
677ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
678{
679 struct msghdr msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
680 struct kvec iov;
681 int len;
682
683 EnterFunction(7);
684 iov.iov_base = (void *)buffer;
685 iov.iov_len = length;
686
687 len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
688
689 LeaveFunction(7);
690 return len;
691}
692
693static void
694ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
695{
696 int msize;
697
698 msize = msg->size;
699
700 /* Put size in network byte order */
701 msg->size = htons(msg->size);
702
703 if (ip_vs_send_async(sock, (char *)msg, msize) != msize)
704 IP_VS_ERR("ip_vs_send_async error\n");
705}
706
707static int
708ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
709{
710 struct msghdr msg = {NULL,};
711 struct kvec iov;
712 int len;
713
714 EnterFunction(7);
715
716 /* Receive a packet */
717 iov.iov_base = buffer;
718 iov.iov_len = (size_t)buflen;
719
720 len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, 0);
721
722 if (len < 0)
723 return -1;
724
725 LeaveFunction(7);
726 return len;
727}
728
729
Sven Wegener998e7a72008-07-16 11:13:50 +0000730static int sync_thread_master(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Sven Wegener998e7a72008-07-16 11:13:50 +0000732 struct ip_vs_sync_thread_data *tinfo = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 struct ip_vs_sync_buff *sb;
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, "
736 "syncid = %d\n",
737 ip_vs_master_mcast_ifn, ip_vs_master_syncid);
738
Sven Wegener998e7a72008-07-16 11:13:50 +0000739 while (!kthread_should_stop()) {
740 while ((sb = sb_dequeue())) {
741 ip_vs_send_sync_msg(tinfo->sock, sb->mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 ip_vs_sync_buff_release(sb);
743 }
744
745 /* check if entries stay in curr_sb for 2 seconds */
Sven Wegener998e7a72008-07-16 11:13:50 +0000746 sb = get_curr_sync_buff(2 * HZ);
747 if (sb) {
748 ip_vs_send_sync_msg(tinfo->sock, sb->mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 ip_vs_sync_buff_release(sb);
750 }
751
Sven Wegener375c6bb2008-07-16 11:14:03 +0000752 schedule_timeout_interruptible(HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754
755 /* clean up the sync_buff queue */
756 while ((sb=sb_dequeue())) {
757 ip_vs_sync_buff_release(sb);
758 }
759
760 /* clean up the current sync_buff */
761 if ((sb = get_curr_sync_buff(0))) {
762 ip_vs_sync_buff_release(sb);
763 }
764
765 /* release the sending multicast socket */
Sven Wegener998e7a72008-07-16 11:13:50 +0000766 sock_release(tinfo->sock);
767 kfree(tinfo);
768
769 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
772
Sven Wegener998e7a72008-07-16 11:13:50 +0000773static int sync_thread_backup(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Sven Wegener998e7a72008-07-16 11:13:50 +0000775 struct ip_vs_sync_thread_data *tinfo = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 int len;
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
779 "syncid = %d\n",
780 ip_vs_backup_mcast_ifn, ip_vs_backup_syncid);
781
Sven Wegener998e7a72008-07-16 11:13:50 +0000782 while (!kthread_should_stop()) {
Sven Wegenerba6fd852008-07-16 11:13:56 +0000783 wait_event_interruptible(*tinfo->sock->sk->sk_sleep,
784 !skb_queue_empty(&tinfo->sock->sk->sk_receive_queue)
785 || kthread_should_stop());
786
Sven Wegener998e7a72008-07-16 11:13:50 +0000787 /* do we have data now? */
788 while (!skb_queue_empty(&(tinfo->sock->sk->sk_receive_queue))) {
789 len = ip_vs_receive(tinfo->sock, tinfo->buf,
790 sync_recv_mesg_maxlen);
791 if (len <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 IP_VS_ERR("receiving message error\n");
793 break;
794 }
Sven Wegener998e7a72008-07-16 11:13:50 +0000795
796 /* disable bottom half, because it accesses the data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 shared by softirq while getting/creating conns */
798 local_bh_disable();
Sven Wegener998e7a72008-07-16 11:13:50 +0000799 ip_vs_process_message(tinfo->buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 local_bh_enable();
801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803
804 /* release the sending multicast socket */
Sven Wegener998e7a72008-07-16 11:13:50 +0000805 sock_release(tinfo->sock);
806 kfree(tinfo->buf);
Neil Hormancc0191a2007-06-18 22:33:20 -0700807 kfree(tinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 return 0;
810}
811
812
813int start_sync_thread(int state, char *mcast_ifn, __u8 syncid)
814{
Neil Hormancc0191a2007-06-18 22:33:20 -0700815 struct ip_vs_sync_thread_data *tinfo;
Sven Wegener998e7a72008-07-16 11:13:50 +0000816 struct task_struct **realtask, *task;
817 struct socket *sock;
818 char *name, *buf = NULL;
819 int (*threadfn)(void *data);
820 int result = -ENOMEM;
Neil Hormancc0191a2007-06-18 22:33:20 -0700821
Harvey Harrison0dc47872008-03-05 20:47:47 -0800822 IP_VS_DBG(7, "%s: pid %d\n", __func__, task_pid_nr(current));
Sven Wegener998e7a72008-07-16 11:13:50 +0000823 IP_VS_DBG(7, "Each ip_vs_sync_conn entry needs %Zd bytes\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 sizeof(struct ip_vs_sync_conn));
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (state == IP_VS_STATE_MASTER) {
Sven Wegener998e7a72008-07-16 11:13:50 +0000827 if (sync_master_thread)
828 return -EEXIST;
829
Simon Horman37004af2006-12-10 22:36:18 -0800830 strlcpy(ip_vs_master_mcast_ifn, mcast_ifn,
831 sizeof(ip_vs_master_mcast_ifn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 ip_vs_master_syncid = syncid;
Sven Wegener998e7a72008-07-16 11:13:50 +0000833 realtask = &sync_master_thread;
834 name = "ipvs_syncmaster";
835 threadfn = sync_thread_master;
836 sock = make_send_sock();
837 } else if (state == IP_VS_STATE_BACKUP) {
838 if (sync_backup_thread)
839 return -EEXIST;
840
Simon Horman37004af2006-12-10 22:36:18 -0800841 strlcpy(ip_vs_backup_mcast_ifn, mcast_ifn,
842 sizeof(ip_vs_backup_mcast_ifn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 ip_vs_backup_syncid = syncid;
Sven Wegener998e7a72008-07-16 11:13:50 +0000844 realtask = &sync_backup_thread;
845 name = "ipvs_syncbackup";
846 threadfn = sync_thread_backup;
847 sock = make_receive_sock();
848 } else {
849 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851
Sven Wegener998e7a72008-07-16 11:13:50 +0000852 if (IS_ERR(sock)) {
853 result = PTR_ERR(sock);
854 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
Sven Wegener998e7a72008-07-16 11:13:50 +0000857 set_sync_mesg_maxlen(state);
858 if (state == IP_VS_STATE_BACKUP) {
859 buf = kmalloc(sync_recv_mesg_maxlen, GFP_KERNEL);
860 if (!buf)
861 goto outsocket;
862 }
863
864 tinfo = kmalloc(sizeof(*tinfo), GFP_KERNEL);
865 if (!tinfo)
866 goto outbuf;
867
868 tinfo->sock = sock;
869 tinfo->buf = buf;
870
871 task = kthread_run(threadfn, tinfo, name);
872 if (IS_ERR(task)) {
873 result = PTR_ERR(task);
874 goto outtinfo;
875 }
876
877 /* mark as active */
878 *realtask = task;
879 ip_vs_sync_state |= state;
880
881 /* increase the module use count */
882 ip_vs_use_count_inc();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 return 0;
Sven Wegener998e7a72008-07-16 11:13:50 +0000885
886outtinfo:
887 kfree(tinfo);
888outbuf:
889 kfree(buf);
890outsocket:
891 sock_release(sock);
892out:
893 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896
897int stop_sync_thread(int state)
898{
Harvey Harrison0dc47872008-03-05 20:47:47 -0800899 IP_VS_DBG(7, "%s: pid %d\n", __func__, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Sven Wegener998e7a72008-07-16 11:13:50 +0000901 if (state == IP_VS_STATE_MASTER) {
902 if (!sync_master_thread)
903 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Sven Wegener998e7a72008-07-16 11:13:50 +0000905 IP_VS_INFO("stopping master sync thread %d ...\n",
906 task_pid_nr(sync_master_thread));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Sven Wegener998e7a72008-07-16 11:13:50 +0000908 /*
909 * The lock synchronizes with sb_queue_tail(), so that we don't
910 * add sync buffers to the queue, when we are already in
911 * progress of stopping the master sync daemon.
912 */
913
Sven Wegenerbc0fde22008-08-10 09:14:05 +0000914 spin_lock_bh(&ip_vs_sync_lock);
Sven Wegener998e7a72008-07-16 11:13:50 +0000915 ip_vs_sync_state &= ~IP_VS_STATE_MASTER;
Sven Wegenerbc0fde22008-08-10 09:14:05 +0000916 spin_unlock_bh(&ip_vs_sync_lock);
Sven Wegener998e7a72008-07-16 11:13:50 +0000917 kthread_stop(sync_master_thread);
918 sync_master_thread = NULL;
919 } else if (state == IP_VS_STATE_BACKUP) {
920 if (!sync_backup_thread)
921 return -ESRCH;
922
923 IP_VS_INFO("stopping backup sync thread %d ...\n",
924 task_pid_nr(sync_backup_thread));
925
926 ip_vs_sync_state &= ~IP_VS_STATE_BACKUP;
927 kthread_stop(sync_backup_thread);
928 sync_backup_thread = NULL;
929 } else {
930 return -EINVAL;
931 }
932
933 /* decrease the module use count */
934 ip_vs_use_count_dec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 return 0;
937}