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