ceph: fix statvfs fr_size
[linux-2.6/btrfs-unstable.git] / net / netfilter / ipvs / ip_vs_sync.c
blob44fd10c539ac42dfaa967974051bdfacb17099fb
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.
8 * Version 1, is capable of handling both version 0 and 1 messages.
9 * Version 0 is the plain old format.
10 * Note Version 0 receivers will just drop Ver 1 messages.
11 * Version 1 is capable of handle IPv6, Persistence data,
12 * time-outs, and firewall marks.
13 * In ver.1 "ip_vs_sync_conn_options" will be sent in netw. order.
14 * Ver. 0 can be turned on by sysctl -w net.ipv4.vs.sync_version=0
16 * Definitions Message: is a complete datagram
17 * Sync_conn: is a part of a Message
18 * Param Data is an option to a Sync_conn.
20 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
22 * ip_vs_sync: sync connection info from master load balancer to backups
23 * through multicast
25 * Changes:
26 * Alexandre Cassen : Added master & backup support at a time.
27 * Alexandre Cassen : Added SyncID support for incoming sync
28 * messages filtering.
29 * Justin Ossevoort : Fix endian problem on sync message size.
30 * Hans Schillstrom : Added Version 1: i.e. IPv6,
31 * Persistence support, fwmark and time-out.
34 #define KMSG_COMPONENT "IPVS"
35 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
37 #include <linux/module.h>
38 #include <linux/slab.h>
39 #include <linux/inetdevice.h>
40 #include <linux/net.h>
41 #include <linux/completion.h>
42 #include <linux/delay.h>
43 #include <linux/skbuff.h>
44 #include <linux/in.h>
45 #include <linux/igmp.h> /* for ip_mc_join_group */
46 #include <linux/udp.h>
47 #include <linux/err.h>
48 #include <linux/kthread.h>
49 #include <linux/wait.h>
50 #include <linux/kernel.h>
52 #include <asm/unaligned.h> /* Used for ntoh_seq and hton_seq */
54 #include <net/ip.h>
55 #include <net/sock.h>
57 #include <net/ip_vs.h>
59 #define IP_VS_SYNC_GROUP 0xe0000051 /* multicast addr - 224.0.0.81 */
60 #define IP_VS_SYNC_PORT 8848 /* multicast port */
62 #define SYNC_PROTO_VER 1 /* Protocol version in header */
64 static struct lock_class_key __ipvs_sync_key;
66 * IPVS sync connection entry
67 * Version 0, i.e. original version.
69 struct ip_vs_sync_conn_v0 {
70 __u8 reserved;
72 /* Protocol, addresses and port numbers */
73 __u8 protocol; /* Which protocol (TCP/UDP) */
74 __be16 cport;
75 __be16 vport;
76 __be16 dport;
77 __be32 caddr; /* client address */
78 __be32 vaddr; /* virtual address */
79 __be32 daddr; /* destination address */
81 /* Flags and state transition */
82 __be16 flags; /* status flags */
83 __be16 state; /* state info */
85 /* The sequence options start here */
88 struct ip_vs_sync_conn_options {
89 struct ip_vs_seq in_seq; /* incoming seq. struct */
90 struct ip_vs_seq out_seq; /* outgoing seq. struct */
94 Sync Connection format (sync_conn)
96 0 1 2 3
97 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
98 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99 | Type | Protocol | Ver. | Size |
100 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101 | Flags |
102 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103 | State | cport |
104 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105 | vport | dport |
106 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107 | fwmark |
108 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109 | timeout (in sec.) |
110 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111 | ... |
112 | IP-Addresses (v4 or v6) |
113 | ... |
114 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115 Optional Parameters.
116 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
117 | Param. Type | Param. Length | Param. data |
118 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
119 | ... |
120 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
121 | | Param Type | Param. Length |
122 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
123 | Param data |
124 | Last Param data should be padded for 32 bit alignment |
125 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
129 * Type 0, IPv4 sync connection format
131 struct ip_vs_sync_v4 {
132 __u8 type;
133 __u8 protocol; /* Which protocol (TCP/UDP) */
134 __be16 ver_size; /* Version msb 4 bits */
135 /* Flags and state transition */
136 __be32 flags; /* status flags */
137 __be16 state; /* state info */
138 /* Protocol, addresses and port numbers */
139 __be16 cport;
140 __be16 vport;
141 __be16 dport;
142 __be32 fwmark; /* Firewall mark from skb */
143 __be32 timeout; /* cp timeout */
144 __be32 caddr; /* client address */
145 __be32 vaddr; /* virtual address */
146 __be32 daddr; /* destination address */
147 /* The sequence options start here */
148 /* PE data padded to 32bit alignment after seq. options */
151 * Type 2 messages IPv6
153 struct ip_vs_sync_v6 {
154 __u8 type;
155 __u8 protocol; /* Which protocol (TCP/UDP) */
156 __be16 ver_size; /* Version msb 4 bits */
157 /* Flags and state transition */
158 __be32 flags; /* status flags */
159 __be16 state; /* state info */
160 /* Protocol, addresses and port numbers */
161 __be16 cport;
162 __be16 vport;
163 __be16 dport;
164 __be32 fwmark; /* Firewall mark from skb */
165 __be32 timeout; /* cp timeout */
166 struct in6_addr caddr; /* client address */
167 struct in6_addr vaddr; /* virtual address */
168 struct in6_addr daddr; /* destination address */
169 /* The sequence options start here */
170 /* PE data padded to 32bit alignment after seq. options */
173 union ip_vs_sync_conn {
174 struct ip_vs_sync_v4 v4;
175 struct ip_vs_sync_v6 v6;
178 /* Bits in Type field in above */
179 #define STYPE_INET6 0
180 #define STYPE_F_INET6 (1 << STYPE_INET6)
182 #define SVER_SHIFT 12 /* Shift to get version */
183 #define SVER_MASK 0x0fff /* Mask to strip version */
185 #define IPVS_OPT_SEQ_DATA 1
186 #define IPVS_OPT_PE_DATA 2
187 #define IPVS_OPT_PE_NAME 3
188 #define IPVS_OPT_PARAM 7
190 #define IPVS_OPT_F_SEQ_DATA (1 << (IPVS_OPT_SEQ_DATA-1))
191 #define IPVS_OPT_F_PE_DATA (1 << (IPVS_OPT_PE_DATA-1))
192 #define IPVS_OPT_F_PE_NAME (1 << (IPVS_OPT_PE_NAME-1))
193 #define IPVS_OPT_F_PARAM (1 << (IPVS_OPT_PARAM-1))
195 struct ip_vs_sync_thread_data {
196 struct net *net;
197 struct socket *sock;
198 char *buf;
199 int id;
202 /* Version 0 definition of packet sizes */
203 #define SIMPLE_CONN_SIZE (sizeof(struct ip_vs_sync_conn_v0))
204 #define FULL_CONN_SIZE \
205 (sizeof(struct ip_vs_sync_conn_v0) + sizeof(struct ip_vs_sync_conn_options))
209 The master mulitcasts messages (Datagrams) to the backup load balancers
210 in the following format.
212 Version 1:
213 Note, first byte should be Zero, so ver 0 receivers will drop the packet.
215 0 1 2 3
216 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
217 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
218 | 0 | SyncID | Size |
219 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
220 | Count Conns | Version | Reserved, set to Zero |
221 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
223 | IPVS Sync Connection (1) |
224 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
225 | . |
226 ~ . ~
227 | . |
228 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
230 | IPVS Sync Connection (n) |
231 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
233 Version 0 Header
234 0 1 2 3
235 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
236 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
237 | Count Conns | SyncID | Size |
238 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
239 | IPVS Sync Connection (1) |
242 #define SYNC_MESG_HEADER_LEN 4
243 #define MAX_CONNS_PER_SYNCBUFF 255 /* nr_conns in ip_vs_sync_mesg is 8 bit */
245 /* Version 0 header */
246 struct ip_vs_sync_mesg_v0 {
247 __u8 nr_conns;
248 __u8 syncid;
249 __u16 size;
251 /* ip_vs_sync_conn entries start here */
254 /* Version 1 header */
255 struct ip_vs_sync_mesg {
256 __u8 reserved; /* must be zero */
257 __u8 syncid;
258 __u16 size;
259 __u8 nr_conns;
260 __s8 version; /* SYNC_PROTO_VER */
261 __u16 spare;
262 /* ip_vs_sync_conn entries start here */
265 struct ip_vs_sync_buff {
266 struct list_head list;
267 unsigned long firstuse;
269 /* pointers for the message data */
270 struct ip_vs_sync_mesg *mesg;
271 unsigned char *head;
272 unsigned char *end;
276 * Copy of struct ip_vs_seq
277 * From unaligned network order to aligned host order
279 static void ntoh_seq(struct ip_vs_seq *no, struct ip_vs_seq *ho)
281 ho->init_seq = get_unaligned_be32(&no->init_seq);
282 ho->delta = get_unaligned_be32(&no->delta);
283 ho->previous_delta = get_unaligned_be32(&no->previous_delta);
287 * Copy of struct ip_vs_seq
288 * From Aligned host order to unaligned network order
290 static void hton_seq(struct ip_vs_seq *ho, struct ip_vs_seq *no)
292 put_unaligned_be32(ho->init_seq, &no->init_seq);
293 put_unaligned_be32(ho->delta, &no->delta);
294 put_unaligned_be32(ho->previous_delta, &no->previous_delta);
297 static inline struct ip_vs_sync_buff *
298 sb_dequeue(struct netns_ipvs *ipvs, struct ipvs_master_sync_state *ms)
300 struct ip_vs_sync_buff *sb;
302 spin_lock_bh(&ipvs->sync_lock);
303 if (list_empty(&ms->sync_queue)) {
304 sb = NULL;
305 __set_current_state(TASK_INTERRUPTIBLE);
306 } else {
307 sb = list_entry(ms->sync_queue.next, struct ip_vs_sync_buff,
308 list);
309 list_del(&sb->list);
310 ms->sync_queue_len--;
311 if (!ms->sync_queue_len)
312 ms->sync_queue_delay = 0;
314 spin_unlock_bh(&ipvs->sync_lock);
316 return sb;
320 * Create a new sync buffer for Version 1 proto.
322 static inline struct ip_vs_sync_buff *
323 ip_vs_sync_buff_create(struct netns_ipvs *ipvs)
325 struct ip_vs_sync_buff *sb;
327 if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
328 return NULL;
330 sb->mesg = kmalloc(ipvs->send_mesg_maxlen, GFP_ATOMIC);
331 if (!sb->mesg) {
332 kfree(sb);
333 return NULL;
335 sb->mesg->reserved = 0; /* old nr_conns i.e. must be zero now */
336 sb->mesg->version = SYNC_PROTO_VER;
337 sb->mesg->syncid = ipvs->master_syncid;
338 sb->mesg->size = sizeof(struct ip_vs_sync_mesg);
339 sb->mesg->nr_conns = 0;
340 sb->mesg->spare = 0;
341 sb->head = (unsigned char *)sb->mesg + sizeof(struct ip_vs_sync_mesg);
342 sb->end = (unsigned char *)sb->mesg + ipvs->send_mesg_maxlen;
344 sb->firstuse = jiffies;
345 return sb;
348 static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff *sb)
350 kfree(sb->mesg);
351 kfree(sb);
354 static inline void sb_queue_tail(struct netns_ipvs *ipvs,
355 struct ipvs_master_sync_state *ms)
357 struct ip_vs_sync_buff *sb = ms->sync_buff;
359 spin_lock(&ipvs->sync_lock);
360 if (ipvs->sync_state & IP_VS_STATE_MASTER &&
361 ms->sync_queue_len < sysctl_sync_qlen_max(ipvs)) {
362 if (!ms->sync_queue_len)
363 schedule_delayed_work(&ms->master_wakeup_work,
364 max(IPVS_SYNC_SEND_DELAY, 1));
365 ms->sync_queue_len++;
366 list_add_tail(&sb->list, &ms->sync_queue);
367 if ((++ms->sync_queue_delay) == IPVS_SYNC_WAKEUP_RATE)
368 wake_up_process(ms->master_thread);
369 } else
370 ip_vs_sync_buff_release(sb);
371 spin_unlock(&ipvs->sync_lock);
375 * Get the current sync buffer if it has been created for more
376 * than the specified time or the specified time is zero.
378 static inline struct ip_vs_sync_buff *
379 get_curr_sync_buff(struct netns_ipvs *ipvs, struct ipvs_master_sync_state *ms,
380 unsigned long time)
382 struct ip_vs_sync_buff *sb;
384 spin_lock_bh(&ipvs->sync_buff_lock);
385 sb = ms->sync_buff;
386 if (sb && time_after_eq(jiffies - sb->firstuse, time)) {
387 ms->sync_buff = NULL;
388 __set_current_state(TASK_RUNNING);
389 } else
390 sb = NULL;
391 spin_unlock_bh(&ipvs->sync_buff_lock);
392 return sb;
395 static inline int
396 select_master_thread_id(struct netns_ipvs *ipvs, struct ip_vs_conn *cp)
398 return ((long) cp >> (1 + ilog2(sizeof(*cp)))) & ipvs->threads_mask;
402 * Create a new sync buffer for Version 0 proto.
404 static inline struct ip_vs_sync_buff *
405 ip_vs_sync_buff_create_v0(struct netns_ipvs *ipvs)
407 struct ip_vs_sync_buff *sb;
408 struct ip_vs_sync_mesg_v0 *mesg;
410 if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
411 return NULL;
413 sb->mesg = kmalloc(ipvs->send_mesg_maxlen, GFP_ATOMIC);
414 if (!sb->mesg) {
415 kfree(sb);
416 return NULL;
418 mesg = (struct ip_vs_sync_mesg_v0 *)sb->mesg;
419 mesg->nr_conns = 0;
420 mesg->syncid = ipvs->master_syncid;
421 mesg->size = sizeof(struct ip_vs_sync_mesg_v0);
422 sb->head = (unsigned char *)mesg + sizeof(struct ip_vs_sync_mesg_v0);
423 sb->end = (unsigned char *)mesg + ipvs->send_mesg_maxlen;
424 sb->firstuse = jiffies;
425 return sb;
428 /* Check if conn should be synced.
429 * pkts: conn packets, use sysctl_sync_threshold to avoid packet check
430 * - (1) sync_refresh_period: reduce sync rate. Additionally, retry
431 * sync_retries times with period of sync_refresh_period/8
432 * - (2) if both sync_refresh_period and sync_period are 0 send sync only
433 * for state changes or only once when pkts matches sync_threshold
434 * - (3) templates: rate can be reduced only with sync_refresh_period or
435 * with (2)
437 static int ip_vs_sync_conn_needed(struct netns_ipvs *ipvs,
438 struct ip_vs_conn *cp, int pkts)
440 unsigned long orig = ACCESS_ONCE(cp->sync_endtime);
441 unsigned long now = jiffies;
442 unsigned long n = (now + cp->timeout) & ~3UL;
443 unsigned int sync_refresh_period;
444 int sync_period;
445 int force;
447 /* Check if we sync in current state */
448 if (unlikely(cp->flags & IP_VS_CONN_F_TEMPLATE))
449 force = 0;
450 else if (likely(cp->protocol == IPPROTO_TCP)) {
451 if (!((1 << cp->state) &
452 ((1 << IP_VS_TCP_S_ESTABLISHED) |
453 (1 << IP_VS_TCP_S_FIN_WAIT) |
454 (1 << IP_VS_TCP_S_CLOSE) |
455 (1 << IP_VS_TCP_S_CLOSE_WAIT) |
456 (1 << IP_VS_TCP_S_TIME_WAIT))))
457 return 0;
458 force = cp->state != cp->old_state;
459 if (force && cp->state != IP_VS_TCP_S_ESTABLISHED)
460 goto set;
461 } else if (unlikely(cp->protocol == IPPROTO_SCTP)) {
462 if (!((1 << cp->state) &
463 ((1 << IP_VS_SCTP_S_ESTABLISHED) |
464 (1 << IP_VS_SCTP_S_CLOSED) |
465 (1 << IP_VS_SCTP_S_SHUT_ACK_CLI) |
466 (1 << IP_VS_SCTP_S_SHUT_ACK_SER))))
467 return 0;
468 force = cp->state != cp->old_state;
469 if (force && cp->state != IP_VS_SCTP_S_ESTABLISHED)
470 goto set;
471 } else {
472 /* UDP or another protocol with single state */
473 force = 0;
476 sync_refresh_period = sysctl_sync_refresh_period(ipvs);
477 if (sync_refresh_period > 0) {
478 long diff = n - orig;
479 long min_diff = max(cp->timeout >> 1, 10UL * HZ);
481 /* Avoid sync if difference is below sync_refresh_period
482 * and below the half timeout.
484 if (abs(diff) < min_t(long, sync_refresh_period, min_diff)) {
485 int retries = orig & 3;
487 if (retries >= sysctl_sync_retries(ipvs))
488 return 0;
489 if (time_before(now, orig - cp->timeout +
490 (sync_refresh_period >> 3)))
491 return 0;
492 n |= retries + 1;
495 sync_period = sysctl_sync_period(ipvs);
496 if (sync_period > 0) {
497 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE) &&
498 pkts % sync_period != sysctl_sync_threshold(ipvs))
499 return 0;
500 } else if (sync_refresh_period <= 0 &&
501 pkts != sysctl_sync_threshold(ipvs))
502 return 0;
504 set:
505 cp->old_state = cp->state;
506 n = cmpxchg(&cp->sync_endtime, orig, n);
507 return n == orig || force;
511 * Version 0 , could be switched in by sys_ctl.
512 * Add an ip_vs_conn information into the current sync_buff.
514 static void ip_vs_sync_conn_v0(struct net *net, struct ip_vs_conn *cp,
515 int pkts)
517 struct netns_ipvs *ipvs = net_ipvs(net);
518 struct ip_vs_sync_mesg_v0 *m;
519 struct ip_vs_sync_conn_v0 *s;
520 struct ip_vs_sync_buff *buff;
521 struct ipvs_master_sync_state *ms;
522 int id;
523 int len;
525 if (unlikely(cp->af != AF_INET))
526 return;
527 /* Do not sync ONE PACKET */
528 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
529 return;
531 if (!ip_vs_sync_conn_needed(ipvs, cp, pkts))
532 return;
534 spin_lock(&ipvs->sync_buff_lock);
535 if (!(ipvs->sync_state & IP_VS_STATE_MASTER)) {
536 spin_unlock(&ipvs->sync_buff_lock);
537 return;
540 id = select_master_thread_id(ipvs, cp);
541 ms = &ipvs->ms[id];
542 buff = ms->sync_buff;
543 if (buff) {
544 m = (struct ip_vs_sync_mesg_v0 *) buff->mesg;
545 /* Send buffer if it is for v1 */
546 if (!m->nr_conns) {
547 sb_queue_tail(ipvs, ms);
548 ms->sync_buff = NULL;
549 buff = NULL;
552 if (!buff) {
553 buff = ip_vs_sync_buff_create_v0(ipvs);
554 if (!buff) {
555 spin_unlock(&ipvs->sync_buff_lock);
556 pr_err("ip_vs_sync_buff_create failed.\n");
557 return;
559 ms->sync_buff = buff;
562 len = (cp->flags & IP_VS_CONN_F_SEQ_MASK) ? FULL_CONN_SIZE :
563 SIMPLE_CONN_SIZE;
564 m = (struct ip_vs_sync_mesg_v0 *) buff->mesg;
565 s = (struct ip_vs_sync_conn_v0 *) buff->head;
567 /* copy members */
568 s->reserved = 0;
569 s->protocol = cp->protocol;
570 s->cport = cp->cport;
571 s->vport = cp->vport;
572 s->dport = cp->dport;
573 s->caddr = cp->caddr.ip;
574 s->vaddr = cp->vaddr.ip;
575 s->daddr = cp->daddr.ip;
576 s->flags = htons(cp->flags & ~IP_VS_CONN_F_HASHED);
577 s->state = htons(cp->state);
578 if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
579 struct ip_vs_sync_conn_options *opt =
580 (struct ip_vs_sync_conn_options *)&s[1];
581 memcpy(opt, &cp->in_seq, sizeof(*opt));
584 m->nr_conns++;
585 m->size += len;
586 buff->head += len;
588 /* check if there is a space for next one */
589 if (buff->head + FULL_CONN_SIZE > buff->end) {
590 sb_queue_tail(ipvs, ms);
591 ms->sync_buff = NULL;
593 spin_unlock(&ipvs->sync_buff_lock);
595 /* synchronize its controller if it has */
596 cp = cp->control;
597 if (cp) {
598 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
599 pkts = atomic_add_return(1, &cp->in_pkts);
600 else
601 pkts = sysctl_sync_threshold(ipvs);
602 ip_vs_sync_conn(net, cp->control, pkts);
607 * Add an ip_vs_conn information into the current sync_buff.
608 * Called by ip_vs_in.
609 * Sending Version 1 messages
611 void ip_vs_sync_conn(struct net *net, struct ip_vs_conn *cp, int pkts)
613 struct netns_ipvs *ipvs = net_ipvs(net);
614 struct ip_vs_sync_mesg *m;
615 union ip_vs_sync_conn *s;
616 struct ip_vs_sync_buff *buff;
617 struct ipvs_master_sync_state *ms;
618 int id;
619 __u8 *p;
620 unsigned int len, pe_name_len, pad;
622 /* Handle old version of the protocol */
623 if (sysctl_sync_ver(ipvs) == 0) {
624 ip_vs_sync_conn_v0(net, cp, pkts);
625 return;
627 /* Do not sync ONE PACKET */
628 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
629 goto control;
630 sloop:
631 if (!ip_vs_sync_conn_needed(ipvs, cp, pkts))
632 goto control;
634 /* Sanity checks */
635 pe_name_len = 0;
636 if (cp->pe_data_len) {
637 if (!cp->pe_data || !cp->dest) {
638 IP_VS_ERR_RL("SYNC, connection pe_data invalid\n");
639 return;
641 pe_name_len = strnlen(cp->pe->name, IP_VS_PENAME_MAXLEN);
644 spin_lock(&ipvs->sync_buff_lock);
645 if (!(ipvs->sync_state & IP_VS_STATE_MASTER)) {
646 spin_unlock(&ipvs->sync_buff_lock);
647 return;
650 id = select_master_thread_id(ipvs, cp);
651 ms = &ipvs->ms[id];
653 #ifdef CONFIG_IP_VS_IPV6
654 if (cp->af == AF_INET6)
655 len = sizeof(struct ip_vs_sync_v6);
656 else
657 #endif
658 len = sizeof(struct ip_vs_sync_v4);
660 if (cp->flags & IP_VS_CONN_F_SEQ_MASK)
661 len += sizeof(struct ip_vs_sync_conn_options) + 2;
663 if (cp->pe_data_len)
664 len += cp->pe_data_len + 2; /* + Param hdr field */
665 if (pe_name_len)
666 len += pe_name_len + 2;
668 /* check if there is a space for this one */
669 pad = 0;
670 buff = ms->sync_buff;
671 if (buff) {
672 m = buff->mesg;
673 pad = (4 - (size_t) buff->head) & 3;
674 /* Send buffer if it is for v0 */
675 if (buff->head + len + pad > buff->end || m->reserved) {
676 sb_queue_tail(ipvs, ms);
677 ms->sync_buff = NULL;
678 buff = NULL;
679 pad = 0;
683 if (!buff) {
684 buff = ip_vs_sync_buff_create(ipvs);
685 if (!buff) {
686 spin_unlock(&ipvs->sync_buff_lock);
687 pr_err("ip_vs_sync_buff_create failed.\n");
688 return;
690 ms->sync_buff = buff;
691 m = buff->mesg;
694 p = buff->head;
695 buff->head += pad + len;
696 m->size += pad + len;
697 /* Add ev. padding from prev. sync_conn */
698 while (pad--)
699 *(p++) = 0;
701 s = (union ip_vs_sync_conn *)p;
703 /* Set message type & copy members */
704 s->v4.type = (cp->af == AF_INET6 ? STYPE_F_INET6 : 0);
705 s->v4.ver_size = htons(len & SVER_MASK); /* Version 0 */
706 s->v4.flags = htonl(cp->flags & ~IP_VS_CONN_F_HASHED);
707 s->v4.state = htons(cp->state);
708 s->v4.protocol = cp->protocol;
709 s->v4.cport = cp->cport;
710 s->v4.vport = cp->vport;
711 s->v4.dport = cp->dport;
712 s->v4.fwmark = htonl(cp->fwmark);
713 s->v4.timeout = htonl(cp->timeout / HZ);
714 m->nr_conns++;
716 #ifdef CONFIG_IP_VS_IPV6
717 if (cp->af == AF_INET6) {
718 p += sizeof(struct ip_vs_sync_v6);
719 s->v6.caddr = cp->caddr.in6;
720 s->v6.vaddr = cp->vaddr.in6;
721 s->v6.daddr = cp->daddr.in6;
722 } else
723 #endif
725 p += sizeof(struct ip_vs_sync_v4); /* options ptr */
726 s->v4.caddr = cp->caddr.ip;
727 s->v4.vaddr = cp->vaddr.ip;
728 s->v4.daddr = cp->daddr.ip;
730 if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
731 *(p++) = IPVS_OPT_SEQ_DATA;
732 *(p++) = sizeof(struct ip_vs_sync_conn_options);
733 hton_seq((struct ip_vs_seq *)p, &cp->in_seq);
734 p += sizeof(struct ip_vs_seq);
735 hton_seq((struct ip_vs_seq *)p, &cp->out_seq);
736 p += sizeof(struct ip_vs_seq);
738 /* Handle pe data */
739 if (cp->pe_data_len && cp->pe_data) {
740 *(p++) = IPVS_OPT_PE_DATA;
741 *(p++) = cp->pe_data_len;
742 memcpy(p, cp->pe_data, cp->pe_data_len);
743 p += cp->pe_data_len;
744 if (pe_name_len) {
745 /* Add PE_NAME */
746 *(p++) = IPVS_OPT_PE_NAME;
747 *(p++) = pe_name_len;
748 memcpy(p, cp->pe->name, pe_name_len);
749 p += pe_name_len;
753 spin_unlock(&ipvs->sync_buff_lock);
755 control:
756 /* synchronize its controller if it has */
757 cp = cp->control;
758 if (!cp)
759 return;
760 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
761 pkts = atomic_add_return(1, &cp->in_pkts);
762 else
763 pkts = sysctl_sync_threshold(ipvs);
764 goto sloop;
768 * fill_param used by version 1
770 static inline int
771 ip_vs_conn_fill_param_sync(struct net *net, int af, union ip_vs_sync_conn *sc,
772 struct ip_vs_conn_param *p,
773 __u8 *pe_data, unsigned int pe_data_len,
774 __u8 *pe_name, unsigned int pe_name_len)
776 #ifdef CONFIG_IP_VS_IPV6
777 if (af == AF_INET6)
778 ip_vs_conn_fill_param(net, af, sc->v6.protocol,
779 (const union nf_inet_addr *)&sc->v6.caddr,
780 sc->v6.cport,
781 (const union nf_inet_addr *)&sc->v6.vaddr,
782 sc->v6.vport, p);
783 else
784 #endif
785 ip_vs_conn_fill_param(net, af, sc->v4.protocol,
786 (const union nf_inet_addr *)&sc->v4.caddr,
787 sc->v4.cport,
788 (const union nf_inet_addr *)&sc->v4.vaddr,
789 sc->v4.vport, p);
790 /* Handle pe data */
791 if (pe_data_len) {
792 if (pe_name_len) {
793 char buff[IP_VS_PENAME_MAXLEN+1];
795 memcpy(buff, pe_name, pe_name_len);
796 buff[pe_name_len]=0;
797 p->pe = __ip_vs_pe_getbyname(buff);
798 if (!p->pe) {
799 IP_VS_DBG(3, "BACKUP, no %s engine found/loaded\n",
800 buff);
801 return 1;
803 } else {
804 IP_VS_ERR_RL("BACKUP, Invalid PE parameters\n");
805 return 1;
808 p->pe_data = kmemdup(pe_data, pe_data_len, GFP_ATOMIC);
809 if (!p->pe_data) {
810 if (p->pe->module)
811 module_put(p->pe->module);
812 return -ENOMEM;
814 p->pe_data_len = pe_data_len;
816 return 0;
820 * Connection Add / Update.
821 * Common for version 0 and 1 reception of backup sync_conns.
822 * Param: ...
823 * timeout is in sec.
825 static void ip_vs_proc_conn(struct net *net, struct ip_vs_conn_param *param,
826 unsigned int flags, unsigned int state,
827 unsigned int protocol, unsigned int type,
828 const union nf_inet_addr *daddr, __be16 dport,
829 unsigned long timeout, __u32 fwmark,
830 struct ip_vs_sync_conn_options *opt)
832 struct ip_vs_dest *dest;
833 struct ip_vs_conn *cp;
834 struct netns_ipvs *ipvs = net_ipvs(net);
836 if (!(flags & IP_VS_CONN_F_TEMPLATE))
837 cp = ip_vs_conn_in_get(param);
838 else
839 cp = ip_vs_ct_in_get(param);
841 if (cp) {
842 /* Free pe_data */
843 kfree(param->pe_data);
845 dest = cp->dest;
846 spin_lock(&cp->lock);
847 if ((cp->flags ^ flags) & IP_VS_CONN_F_INACTIVE &&
848 !(flags & IP_VS_CONN_F_TEMPLATE) && dest) {
849 if (flags & IP_VS_CONN_F_INACTIVE) {
850 atomic_dec(&dest->activeconns);
851 atomic_inc(&dest->inactconns);
852 } else {
853 atomic_inc(&dest->activeconns);
854 atomic_dec(&dest->inactconns);
857 flags &= IP_VS_CONN_F_BACKUP_UPD_MASK;
858 flags |= cp->flags & ~IP_VS_CONN_F_BACKUP_UPD_MASK;
859 cp->flags = flags;
860 spin_unlock(&cp->lock);
861 if (!dest) {
862 dest = ip_vs_try_bind_dest(cp);
863 if (dest)
864 atomic_dec(&dest->refcnt);
866 } else {
868 * Find the appropriate destination for the connection.
869 * If it is not found the connection will remain unbound
870 * but still handled.
872 dest = ip_vs_find_dest(net, type, daddr, dport, param->vaddr,
873 param->vport, protocol, fwmark, flags);
875 cp = ip_vs_conn_new(param, daddr, dport, flags, dest, fwmark);
876 if (dest)
877 atomic_dec(&dest->refcnt);
878 if (!cp) {
879 if (param->pe_data)
880 kfree(param->pe_data);
881 IP_VS_DBG(2, "BACKUP, add new conn. failed\n");
882 return;
886 if (opt)
887 memcpy(&cp->in_seq, opt, sizeof(*opt));
888 atomic_set(&cp->in_pkts, sysctl_sync_threshold(ipvs));
889 cp->state = state;
890 cp->old_state = cp->state;
892 * For Ver 0 messages style
893 * - Not possible to recover the right timeout for templates
894 * - can not find the right fwmark
895 * virtual service. If needed, we can do it for
896 * non-fwmark persistent services.
897 * Ver 1 messages style.
898 * - No problem.
900 if (timeout) {
901 if (timeout > MAX_SCHEDULE_TIMEOUT / HZ)
902 timeout = MAX_SCHEDULE_TIMEOUT / HZ;
903 cp->timeout = timeout*HZ;
904 } else {
905 struct ip_vs_proto_data *pd;
907 pd = ip_vs_proto_data_get(net, protocol);
908 if (!(flags & IP_VS_CONN_F_TEMPLATE) && pd && pd->timeout_table)
909 cp->timeout = pd->timeout_table[state];
910 else
911 cp->timeout = (3*60*HZ);
913 ip_vs_conn_put(cp);
917 * Process received multicast message for Version 0
919 static void ip_vs_process_message_v0(struct net *net, const char *buffer,
920 const size_t buflen)
922 struct ip_vs_sync_mesg_v0 *m = (struct ip_vs_sync_mesg_v0 *)buffer;
923 struct ip_vs_sync_conn_v0 *s;
924 struct ip_vs_sync_conn_options *opt;
925 struct ip_vs_protocol *pp;
926 struct ip_vs_conn_param param;
927 char *p;
928 int i;
930 p = (char *)buffer + sizeof(struct ip_vs_sync_mesg_v0);
931 for (i=0; i<m->nr_conns; i++) {
932 unsigned int flags, state;
934 if (p + SIMPLE_CONN_SIZE > buffer+buflen) {
935 IP_VS_ERR_RL("BACKUP v0, bogus conn\n");
936 return;
938 s = (struct ip_vs_sync_conn_v0 *) p;
939 flags = ntohs(s->flags) | IP_VS_CONN_F_SYNC;
940 flags &= ~IP_VS_CONN_F_HASHED;
941 if (flags & IP_VS_CONN_F_SEQ_MASK) {
942 opt = (struct ip_vs_sync_conn_options *)&s[1];
943 p += FULL_CONN_SIZE;
944 if (p > buffer+buflen) {
945 IP_VS_ERR_RL("BACKUP v0, Dropping buffer bogus conn options\n");
946 return;
948 } else {
949 opt = NULL;
950 p += SIMPLE_CONN_SIZE;
953 state = ntohs(s->state);
954 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
955 pp = ip_vs_proto_get(s->protocol);
956 if (!pp) {
957 IP_VS_DBG(2, "BACKUP v0, Unsupported protocol %u\n",
958 s->protocol);
959 continue;
961 if (state >= pp->num_states) {
962 IP_VS_DBG(2, "BACKUP v0, Invalid %s state %u\n",
963 pp->name, state);
964 continue;
966 } else {
967 /* protocol in templates is not used for state/timeout */
968 if (state > 0) {
969 IP_VS_DBG(2, "BACKUP v0, Invalid template state %u\n",
970 state);
971 state = 0;
975 ip_vs_conn_fill_param(net, AF_INET, s->protocol,
976 (const union nf_inet_addr *)&s->caddr,
977 s->cport,
978 (const union nf_inet_addr *)&s->vaddr,
979 s->vport, &param);
981 /* Send timeout as Zero */
982 ip_vs_proc_conn(net, &param, flags, state, s->protocol, AF_INET,
983 (union nf_inet_addr *)&s->daddr, s->dport,
984 0, 0, opt);
989 * Handle options
991 static inline int ip_vs_proc_seqopt(__u8 *p, unsigned int plen,
992 __u32 *opt_flags,
993 struct ip_vs_sync_conn_options *opt)
995 struct ip_vs_sync_conn_options *topt;
997 topt = (struct ip_vs_sync_conn_options *)p;
999 if (plen != sizeof(struct ip_vs_sync_conn_options)) {
1000 IP_VS_DBG(2, "BACKUP, bogus conn options length\n");
1001 return -EINVAL;
1003 if (*opt_flags & IPVS_OPT_F_SEQ_DATA) {
1004 IP_VS_DBG(2, "BACKUP, conn options found twice\n");
1005 return -EINVAL;
1007 ntoh_seq(&topt->in_seq, &opt->in_seq);
1008 ntoh_seq(&topt->out_seq, &opt->out_seq);
1009 *opt_flags |= IPVS_OPT_F_SEQ_DATA;
1010 return 0;
1013 static int ip_vs_proc_str(__u8 *p, unsigned int plen, unsigned int *data_len,
1014 __u8 **data, unsigned int maxlen,
1015 __u32 *opt_flags, __u32 flag)
1017 if (plen > maxlen) {
1018 IP_VS_DBG(2, "BACKUP, bogus par.data len > %d\n", maxlen);
1019 return -EINVAL;
1021 if (*opt_flags & flag) {
1022 IP_VS_DBG(2, "BACKUP, Par.data found twice 0x%x\n", flag);
1023 return -EINVAL;
1025 *data_len = plen;
1026 *data = p;
1027 *opt_flags |= flag;
1028 return 0;
1031 * Process a Version 1 sync. connection
1033 static inline int ip_vs_proc_sync_conn(struct net *net, __u8 *p, __u8 *msg_end)
1035 struct ip_vs_sync_conn_options opt;
1036 union ip_vs_sync_conn *s;
1037 struct ip_vs_protocol *pp;
1038 struct ip_vs_conn_param param;
1039 __u32 flags;
1040 unsigned int af, state, pe_data_len=0, pe_name_len=0;
1041 __u8 *pe_data=NULL, *pe_name=NULL;
1042 __u32 opt_flags=0;
1043 int retc=0;
1045 s = (union ip_vs_sync_conn *) p;
1047 if (s->v6.type & STYPE_F_INET6) {
1048 #ifdef CONFIG_IP_VS_IPV6
1049 af = AF_INET6;
1050 p += sizeof(struct ip_vs_sync_v6);
1051 #else
1052 IP_VS_DBG(3,"BACKUP, IPv6 msg received, and IPVS is not compiled for IPv6\n");
1053 retc = 10;
1054 goto out;
1055 #endif
1056 } else if (!s->v4.type) {
1057 af = AF_INET;
1058 p += sizeof(struct ip_vs_sync_v4);
1059 } else {
1060 return -10;
1062 if (p > msg_end)
1063 return -20;
1065 /* Process optional params check Type & Len. */
1066 while (p < msg_end) {
1067 int ptype;
1068 int plen;
1070 if (p+2 > msg_end)
1071 return -30;
1072 ptype = *(p++);
1073 plen = *(p++);
1075 if (!plen || ((p + plen) > msg_end))
1076 return -40;
1077 /* Handle seq option p = param data */
1078 switch (ptype & ~IPVS_OPT_F_PARAM) {
1079 case IPVS_OPT_SEQ_DATA:
1080 if (ip_vs_proc_seqopt(p, plen, &opt_flags, &opt))
1081 return -50;
1082 break;
1084 case IPVS_OPT_PE_DATA:
1085 if (ip_vs_proc_str(p, plen, &pe_data_len, &pe_data,
1086 IP_VS_PEDATA_MAXLEN, &opt_flags,
1087 IPVS_OPT_F_PE_DATA))
1088 return -60;
1089 break;
1091 case IPVS_OPT_PE_NAME:
1092 if (ip_vs_proc_str(p, plen,&pe_name_len, &pe_name,
1093 IP_VS_PENAME_MAXLEN, &opt_flags,
1094 IPVS_OPT_F_PE_NAME))
1095 return -70;
1096 break;
1098 default:
1099 /* Param data mandatory ? */
1100 if (!(ptype & IPVS_OPT_F_PARAM)) {
1101 IP_VS_DBG(3, "BACKUP, Unknown mandatory param %d found\n",
1102 ptype & ~IPVS_OPT_F_PARAM);
1103 retc = 20;
1104 goto out;
1107 p += plen; /* Next option */
1110 /* Get flags and Mask off unsupported */
1111 flags = ntohl(s->v4.flags) & IP_VS_CONN_F_BACKUP_MASK;
1112 flags |= IP_VS_CONN_F_SYNC;
1113 state = ntohs(s->v4.state);
1115 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
1116 pp = ip_vs_proto_get(s->v4.protocol);
1117 if (!pp) {
1118 IP_VS_DBG(3,"BACKUP, Unsupported protocol %u\n",
1119 s->v4.protocol);
1120 retc = 30;
1121 goto out;
1123 if (state >= pp->num_states) {
1124 IP_VS_DBG(3, "BACKUP, Invalid %s state %u\n",
1125 pp->name, state);
1126 retc = 40;
1127 goto out;
1129 } else {
1130 /* protocol in templates is not used for state/timeout */
1131 if (state > 0) {
1132 IP_VS_DBG(3, "BACKUP, Invalid template state %u\n",
1133 state);
1134 state = 0;
1137 if (ip_vs_conn_fill_param_sync(net, af, s, &param, pe_data,
1138 pe_data_len, pe_name, pe_name_len)) {
1139 retc = 50;
1140 goto out;
1142 /* If only IPv4, just silent skip IPv6 */
1143 if (af == AF_INET)
1144 ip_vs_proc_conn(net, &param, flags, state, s->v4.protocol, af,
1145 (union nf_inet_addr *)&s->v4.daddr, s->v4.dport,
1146 ntohl(s->v4.timeout), ntohl(s->v4.fwmark),
1147 (opt_flags & IPVS_OPT_F_SEQ_DATA ? &opt : NULL)
1149 #ifdef CONFIG_IP_VS_IPV6
1150 else
1151 ip_vs_proc_conn(net, &param, flags, state, s->v6.protocol, af,
1152 (union nf_inet_addr *)&s->v6.daddr, s->v6.dport,
1153 ntohl(s->v6.timeout), ntohl(s->v6.fwmark),
1154 (opt_flags & IPVS_OPT_F_SEQ_DATA ? &opt : NULL)
1156 #endif
1157 return 0;
1158 /* Error exit */
1159 out:
1160 IP_VS_DBG(2, "BACKUP, Single msg dropped err:%d\n", retc);
1161 return retc;
1165 * Process received multicast message and create the corresponding
1166 * ip_vs_conn entries.
1167 * Handles Version 0 & 1
1169 static void ip_vs_process_message(struct net *net, __u8 *buffer,
1170 const size_t buflen)
1172 struct netns_ipvs *ipvs = net_ipvs(net);
1173 struct ip_vs_sync_mesg *m2 = (struct ip_vs_sync_mesg *)buffer;
1174 __u8 *p, *msg_end;
1175 int i, nr_conns;
1177 if (buflen < sizeof(struct ip_vs_sync_mesg_v0)) {
1178 IP_VS_DBG(2, "BACKUP, message header too short\n");
1179 return;
1181 /* Convert size back to host byte order */
1182 m2->size = ntohs(m2->size);
1184 if (buflen != m2->size) {
1185 IP_VS_DBG(2, "BACKUP, bogus message size\n");
1186 return;
1188 /* SyncID sanity check */
1189 if (ipvs->backup_syncid != 0 && m2->syncid != ipvs->backup_syncid) {
1190 IP_VS_DBG(7, "BACKUP, Ignoring syncid = %d\n", m2->syncid);
1191 return;
1193 /* Handle version 1 message */
1194 if ((m2->version == SYNC_PROTO_VER) && (m2->reserved == 0)
1195 && (m2->spare == 0)) {
1197 msg_end = buffer + sizeof(struct ip_vs_sync_mesg);
1198 nr_conns = m2->nr_conns;
1200 for (i=0; i<nr_conns; i++) {
1201 union ip_vs_sync_conn *s;
1202 unsigned int size;
1203 int retc;
1205 p = msg_end;
1206 if (p + sizeof(s->v4) > buffer+buflen) {
1207 IP_VS_ERR_RL("BACKUP, Dropping buffer, to small\n");
1208 return;
1210 s = (union ip_vs_sync_conn *)p;
1211 size = ntohs(s->v4.ver_size) & SVER_MASK;
1212 msg_end = p + size;
1213 /* Basic sanity checks */
1214 if (msg_end > buffer+buflen) {
1215 IP_VS_ERR_RL("BACKUP, Dropping buffer, msg > buffer\n");
1216 return;
1218 if (ntohs(s->v4.ver_size) >> SVER_SHIFT) {
1219 IP_VS_ERR_RL("BACKUP, Dropping buffer, Unknown version %d\n",
1220 ntohs(s->v4.ver_size) >> SVER_SHIFT);
1221 return;
1223 /* Process a single sync_conn */
1224 retc = ip_vs_proc_sync_conn(net, p, msg_end);
1225 if (retc < 0) {
1226 IP_VS_ERR_RL("BACKUP, Dropping buffer, Err: %d in decoding\n",
1227 retc);
1228 return;
1230 /* Make sure we have 32 bit alignment */
1231 msg_end = p + ((size + 3) & ~3);
1233 } else {
1234 /* Old type of message */
1235 ip_vs_process_message_v0(net, buffer, buflen);
1236 return;
1242 * Setup sndbuf (mode=1) or rcvbuf (mode=0)
1244 static void set_sock_size(struct sock *sk, int mode, int val)
1246 /* setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &val, sizeof(val)); */
1247 /* setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)); */
1248 lock_sock(sk);
1249 if (mode) {
1250 val = clamp_t(int, val, (SOCK_MIN_SNDBUF + 1) / 2,
1251 sysctl_wmem_max);
1252 sk->sk_sndbuf = val * 2;
1253 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
1254 } else {
1255 val = clamp_t(int, val, (SOCK_MIN_RCVBUF + 1) / 2,
1256 sysctl_rmem_max);
1257 sk->sk_rcvbuf = val * 2;
1258 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
1260 release_sock(sk);
1264 * Setup loopback of outgoing multicasts on a sending socket
1266 static void set_mcast_loop(struct sock *sk, u_char loop)
1268 struct inet_sock *inet = inet_sk(sk);
1270 /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
1271 lock_sock(sk);
1272 inet->mc_loop = loop ? 1 : 0;
1273 release_sock(sk);
1277 * Specify TTL for outgoing multicasts on a sending socket
1279 static void set_mcast_ttl(struct sock *sk, u_char ttl)
1281 struct inet_sock *inet = inet_sk(sk);
1283 /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
1284 lock_sock(sk);
1285 inet->mc_ttl = ttl;
1286 release_sock(sk);
1290 * Specifiy default interface for outgoing multicasts
1292 static int set_mcast_if(struct sock *sk, char *ifname)
1294 struct net_device *dev;
1295 struct inet_sock *inet = inet_sk(sk);
1296 struct net *net = sock_net(sk);
1298 dev = __dev_get_by_name(net, ifname);
1299 if (!dev)
1300 return -ENODEV;
1302 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
1303 return -EINVAL;
1305 lock_sock(sk);
1306 inet->mc_index = dev->ifindex;
1307 /* inet->mc_addr = 0; */
1308 release_sock(sk);
1310 return 0;
1315 * Set the maximum length of sync message according to the
1316 * specified interface's MTU.
1318 static int set_sync_mesg_maxlen(struct net *net, int sync_state)
1320 struct netns_ipvs *ipvs = net_ipvs(net);
1321 struct net_device *dev;
1322 int num;
1324 if (sync_state == IP_VS_STATE_MASTER) {
1325 dev = __dev_get_by_name(net, ipvs->master_mcast_ifn);
1326 if (!dev)
1327 return -ENODEV;
1329 num = (dev->mtu - sizeof(struct iphdr) -
1330 sizeof(struct udphdr) -
1331 SYNC_MESG_HEADER_LEN - 20) / SIMPLE_CONN_SIZE;
1332 ipvs->send_mesg_maxlen = SYNC_MESG_HEADER_LEN +
1333 SIMPLE_CONN_SIZE * min(num, MAX_CONNS_PER_SYNCBUFF);
1334 IP_VS_DBG(7, "setting the maximum length of sync sending "
1335 "message %d.\n", ipvs->send_mesg_maxlen);
1336 } else if (sync_state == IP_VS_STATE_BACKUP) {
1337 dev = __dev_get_by_name(net, ipvs->backup_mcast_ifn);
1338 if (!dev)
1339 return -ENODEV;
1341 ipvs->recv_mesg_maxlen = dev->mtu -
1342 sizeof(struct iphdr) - sizeof(struct udphdr);
1343 IP_VS_DBG(7, "setting the maximum length of sync receiving "
1344 "message %d.\n", ipvs->recv_mesg_maxlen);
1347 return 0;
1352 * Join a multicast group.
1353 * the group is specified by a class D multicast address 224.0.0.0/8
1354 * in the in_addr structure passed in as a parameter.
1356 static int
1357 join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
1359 struct net *net = sock_net(sk);
1360 struct ip_mreqn mreq;
1361 struct net_device *dev;
1362 int ret;
1364 memset(&mreq, 0, sizeof(mreq));
1365 memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
1367 dev = __dev_get_by_name(net, ifname);
1368 if (!dev)
1369 return -ENODEV;
1370 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
1371 return -EINVAL;
1373 mreq.imr_ifindex = dev->ifindex;
1375 lock_sock(sk);
1376 ret = ip_mc_join_group(sk, &mreq);
1377 release_sock(sk);
1379 return ret;
1383 static int bind_mcastif_addr(struct socket *sock, char *ifname)
1385 struct net *net = sock_net(sock->sk);
1386 struct net_device *dev;
1387 __be32 addr;
1388 struct sockaddr_in sin;
1390 dev = __dev_get_by_name(net, ifname);
1391 if (!dev)
1392 return -ENODEV;
1394 addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
1395 if (!addr)
1396 pr_err("You probably need to specify IP address on "
1397 "multicast interface.\n");
1399 IP_VS_DBG(7, "binding socket with (%s) %pI4\n",
1400 ifname, &addr);
1402 /* Now bind the socket with the address of multicast interface */
1403 sin.sin_family = AF_INET;
1404 sin.sin_addr.s_addr = addr;
1405 sin.sin_port = 0;
1407 return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
1411 * Set up sending multicast socket over UDP
1413 static struct socket *make_send_sock(struct net *net, int id)
1415 struct netns_ipvs *ipvs = net_ipvs(net);
1416 /* multicast addr */
1417 struct sockaddr_in mcast_addr = {
1418 .sin_family = AF_INET,
1419 .sin_port = cpu_to_be16(IP_VS_SYNC_PORT + id),
1420 .sin_addr.s_addr = cpu_to_be32(IP_VS_SYNC_GROUP),
1422 struct socket *sock;
1423 int result;
1425 /* First create a socket move it to right name space later */
1426 result = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1427 if (result < 0) {
1428 pr_err("Error during creation of socket; terminating\n");
1429 return ERR_PTR(result);
1432 * Kernel sockets that are a part of a namespace, should not
1433 * hold a reference to a namespace in order to allow to stop it.
1434 * After sk_change_net should be released using sk_release_kernel.
1436 sk_change_net(sock->sk, net);
1437 result = set_mcast_if(sock->sk, ipvs->master_mcast_ifn);
1438 if (result < 0) {
1439 pr_err("Error setting outbound mcast interface\n");
1440 goto error;
1443 set_mcast_loop(sock->sk, 0);
1444 set_mcast_ttl(sock->sk, 1);
1445 result = sysctl_sync_sock_size(ipvs);
1446 if (result > 0)
1447 set_sock_size(sock->sk, 1, result);
1449 result = bind_mcastif_addr(sock, ipvs->master_mcast_ifn);
1450 if (result < 0) {
1451 pr_err("Error binding address of the mcast interface\n");
1452 goto error;
1455 result = sock->ops->connect(sock, (struct sockaddr *) &mcast_addr,
1456 sizeof(struct sockaddr), 0);
1457 if (result < 0) {
1458 pr_err("Error connecting to the multicast addr\n");
1459 goto error;
1462 return sock;
1464 error:
1465 sk_release_kernel(sock->sk);
1466 return ERR_PTR(result);
1471 * Set up receiving multicast socket over UDP
1473 static struct socket *make_receive_sock(struct net *net, int id)
1475 struct netns_ipvs *ipvs = net_ipvs(net);
1476 /* multicast addr */
1477 struct sockaddr_in mcast_addr = {
1478 .sin_family = AF_INET,
1479 .sin_port = cpu_to_be16(IP_VS_SYNC_PORT + id),
1480 .sin_addr.s_addr = cpu_to_be32(IP_VS_SYNC_GROUP),
1482 struct socket *sock;
1483 int result;
1485 /* First create a socket */
1486 result = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1487 if (result < 0) {
1488 pr_err("Error during creation of socket; terminating\n");
1489 return ERR_PTR(result);
1492 * Kernel sockets that are a part of a namespace, should not
1493 * hold a reference to a namespace in order to allow to stop it.
1494 * After sk_change_net should be released using sk_release_kernel.
1496 sk_change_net(sock->sk, net);
1497 /* it is equivalent to the REUSEADDR option in user-space */
1498 sock->sk->sk_reuse = SK_CAN_REUSE;
1499 result = sysctl_sync_sock_size(ipvs);
1500 if (result > 0)
1501 set_sock_size(sock->sk, 0, result);
1503 result = sock->ops->bind(sock, (struct sockaddr *) &mcast_addr,
1504 sizeof(struct sockaddr));
1505 if (result < 0) {
1506 pr_err("Error binding to the multicast addr\n");
1507 goto error;
1510 /* join the multicast group */
1511 result = join_mcast_group(sock->sk,
1512 (struct in_addr *) &mcast_addr.sin_addr,
1513 ipvs->backup_mcast_ifn);
1514 if (result < 0) {
1515 pr_err("Error joining to the multicast group\n");
1516 goto error;
1519 return sock;
1521 error:
1522 sk_release_kernel(sock->sk);
1523 return ERR_PTR(result);
1527 static int
1528 ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
1530 struct msghdr msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
1531 struct kvec iov;
1532 int len;
1534 EnterFunction(7);
1535 iov.iov_base = (void *)buffer;
1536 iov.iov_len = length;
1538 len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
1540 LeaveFunction(7);
1541 return len;
1544 static int
1545 ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
1547 int msize;
1548 int ret;
1550 msize = msg->size;
1552 /* Put size in network byte order */
1553 msg->size = htons(msg->size);
1555 ret = ip_vs_send_async(sock, (char *)msg, msize);
1556 if (ret >= 0 || ret == -EAGAIN)
1557 return ret;
1558 pr_err("ip_vs_send_async error %d\n", ret);
1559 return 0;
1562 static int
1563 ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
1565 struct msghdr msg = {NULL,};
1566 struct kvec iov;
1567 int len;
1569 EnterFunction(7);
1571 /* Receive a packet */
1572 iov.iov_base = buffer;
1573 iov.iov_len = (size_t)buflen;
1575 len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, MSG_DONTWAIT);
1577 if (len < 0)
1578 return len;
1580 LeaveFunction(7);
1581 return len;
1584 /* Wakeup the master thread for sending */
1585 static void master_wakeup_work_handler(struct work_struct *work)
1587 struct ipvs_master_sync_state *ms =
1588 container_of(work, struct ipvs_master_sync_state,
1589 master_wakeup_work.work);
1590 struct netns_ipvs *ipvs = ms->ipvs;
1592 spin_lock_bh(&ipvs->sync_lock);
1593 if (ms->sync_queue_len &&
1594 ms->sync_queue_delay < IPVS_SYNC_WAKEUP_RATE) {
1595 ms->sync_queue_delay = IPVS_SYNC_WAKEUP_RATE;
1596 wake_up_process(ms->master_thread);
1598 spin_unlock_bh(&ipvs->sync_lock);
1601 /* Get next buffer to send */
1602 static inline struct ip_vs_sync_buff *
1603 next_sync_buff(struct netns_ipvs *ipvs, struct ipvs_master_sync_state *ms)
1605 struct ip_vs_sync_buff *sb;
1607 sb = sb_dequeue(ipvs, ms);
1608 if (sb)
1609 return sb;
1610 /* Do not delay entries in buffer for more than 2 seconds */
1611 return get_curr_sync_buff(ipvs, ms, IPVS_SYNC_FLUSH_TIME);
1614 static int sync_thread_master(void *data)
1616 struct ip_vs_sync_thread_data *tinfo = data;
1617 struct netns_ipvs *ipvs = net_ipvs(tinfo->net);
1618 struct ipvs_master_sync_state *ms = &ipvs->ms[tinfo->id];
1619 struct sock *sk = tinfo->sock->sk;
1620 struct ip_vs_sync_buff *sb;
1622 pr_info("sync thread started: state = MASTER, mcast_ifn = %s, "
1623 "syncid = %d, id = %d\n",
1624 ipvs->master_mcast_ifn, ipvs->master_syncid, tinfo->id);
1626 for (;;) {
1627 sb = next_sync_buff(ipvs, ms);
1628 if (unlikely(kthread_should_stop()))
1629 break;
1630 if (!sb) {
1631 schedule_timeout(IPVS_SYNC_CHECK_PERIOD);
1632 continue;
1634 while (ip_vs_send_sync_msg(tinfo->sock, sb->mesg) < 0) {
1635 int ret = 0;
1637 __wait_event_interruptible(*sk_sleep(sk),
1638 sock_writeable(sk) ||
1639 kthread_should_stop(),
1640 ret);
1641 if (unlikely(kthread_should_stop()))
1642 goto done;
1644 ip_vs_sync_buff_release(sb);
1647 done:
1648 __set_current_state(TASK_RUNNING);
1649 if (sb)
1650 ip_vs_sync_buff_release(sb);
1652 /* clean up the sync_buff queue */
1653 while ((sb = sb_dequeue(ipvs, ms)))
1654 ip_vs_sync_buff_release(sb);
1655 __set_current_state(TASK_RUNNING);
1657 /* clean up the current sync_buff */
1658 sb = get_curr_sync_buff(ipvs, ms, 0);
1659 if (sb)
1660 ip_vs_sync_buff_release(sb);
1662 /* release the sending multicast socket */
1663 sk_release_kernel(tinfo->sock->sk);
1664 kfree(tinfo);
1666 return 0;
1670 static int sync_thread_backup(void *data)
1672 struct ip_vs_sync_thread_data *tinfo = data;
1673 struct netns_ipvs *ipvs = net_ipvs(tinfo->net);
1674 int len;
1676 pr_info("sync thread started: state = BACKUP, mcast_ifn = %s, "
1677 "syncid = %d, id = %d\n",
1678 ipvs->backup_mcast_ifn, ipvs->backup_syncid, tinfo->id);
1680 while (!kthread_should_stop()) {
1681 wait_event_interruptible(*sk_sleep(tinfo->sock->sk),
1682 !skb_queue_empty(&tinfo->sock->sk->sk_receive_queue)
1683 || kthread_should_stop());
1685 /* do we have data now? */
1686 while (!skb_queue_empty(&(tinfo->sock->sk->sk_receive_queue))) {
1687 len = ip_vs_receive(tinfo->sock, tinfo->buf,
1688 ipvs->recv_mesg_maxlen);
1689 if (len <= 0) {
1690 if (len != -EAGAIN)
1691 pr_err("receiving message error\n");
1692 break;
1695 /* disable bottom half, because it accesses the data
1696 shared by softirq while getting/creating conns */
1697 local_bh_disable();
1698 ip_vs_process_message(tinfo->net, tinfo->buf, len);
1699 local_bh_enable();
1703 /* release the sending multicast socket */
1704 sk_release_kernel(tinfo->sock->sk);
1705 kfree(tinfo->buf);
1706 kfree(tinfo);
1708 return 0;
1712 int start_sync_thread(struct net *net, int state, char *mcast_ifn, __u8 syncid)
1714 struct ip_vs_sync_thread_data *tinfo;
1715 struct task_struct **array = NULL, *task;
1716 struct socket *sock;
1717 struct netns_ipvs *ipvs = net_ipvs(net);
1718 char *name;
1719 int (*threadfn)(void *data);
1720 int id, count;
1721 int result = -ENOMEM;
1723 IP_VS_DBG(7, "%s(): pid %d\n", __func__, task_pid_nr(current));
1724 IP_VS_DBG(7, "Each ip_vs_sync_conn entry needs %Zd bytes\n",
1725 sizeof(struct ip_vs_sync_conn_v0));
1727 if (!ipvs->sync_state) {
1728 count = clamp(sysctl_sync_ports(ipvs), 1, IPVS_SYNC_PORTS_MAX);
1729 ipvs->threads_mask = count - 1;
1730 } else
1731 count = ipvs->threads_mask + 1;
1733 if (state == IP_VS_STATE_MASTER) {
1734 if (ipvs->ms)
1735 return -EEXIST;
1737 strlcpy(ipvs->master_mcast_ifn, mcast_ifn,
1738 sizeof(ipvs->master_mcast_ifn));
1739 ipvs->master_syncid = syncid;
1740 name = "ipvs-m:%d:%d";
1741 threadfn = sync_thread_master;
1742 } else if (state == IP_VS_STATE_BACKUP) {
1743 if (ipvs->backup_threads)
1744 return -EEXIST;
1746 strlcpy(ipvs->backup_mcast_ifn, mcast_ifn,
1747 sizeof(ipvs->backup_mcast_ifn));
1748 ipvs->backup_syncid = syncid;
1749 name = "ipvs-b:%d:%d";
1750 threadfn = sync_thread_backup;
1751 } else {
1752 return -EINVAL;
1755 if (state == IP_VS_STATE_MASTER) {
1756 struct ipvs_master_sync_state *ms;
1758 ipvs->ms = kzalloc(count * sizeof(ipvs->ms[0]), GFP_KERNEL);
1759 if (!ipvs->ms)
1760 goto out;
1761 ms = ipvs->ms;
1762 for (id = 0; id < count; id++, ms++) {
1763 INIT_LIST_HEAD(&ms->sync_queue);
1764 ms->sync_queue_len = 0;
1765 ms->sync_queue_delay = 0;
1766 INIT_DELAYED_WORK(&ms->master_wakeup_work,
1767 master_wakeup_work_handler);
1768 ms->ipvs = ipvs;
1770 } else {
1771 array = kzalloc(count * sizeof(struct task_struct *),
1772 GFP_KERNEL);
1773 if (!array)
1774 goto out;
1776 set_sync_mesg_maxlen(net, state);
1778 tinfo = NULL;
1779 for (id = 0; id < count; id++) {
1780 if (state == IP_VS_STATE_MASTER)
1781 sock = make_send_sock(net, id);
1782 else
1783 sock = make_receive_sock(net, id);
1784 if (IS_ERR(sock)) {
1785 result = PTR_ERR(sock);
1786 goto outtinfo;
1788 tinfo = kmalloc(sizeof(*tinfo), GFP_KERNEL);
1789 if (!tinfo)
1790 goto outsocket;
1791 tinfo->net = net;
1792 tinfo->sock = sock;
1793 if (state == IP_VS_STATE_BACKUP) {
1794 tinfo->buf = kmalloc(ipvs->recv_mesg_maxlen,
1795 GFP_KERNEL);
1796 if (!tinfo->buf)
1797 goto outtinfo;
1798 } else {
1799 tinfo->buf = NULL;
1801 tinfo->id = id;
1803 task = kthread_run(threadfn, tinfo, name, ipvs->gen, id);
1804 if (IS_ERR(task)) {
1805 result = PTR_ERR(task);
1806 goto outtinfo;
1808 tinfo = NULL;
1809 if (state == IP_VS_STATE_MASTER)
1810 ipvs->ms[id].master_thread = task;
1811 else
1812 array[id] = task;
1815 /* mark as active */
1817 if (state == IP_VS_STATE_BACKUP)
1818 ipvs->backup_threads = array;
1819 spin_lock_bh(&ipvs->sync_buff_lock);
1820 ipvs->sync_state |= state;
1821 spin_unlock_bh(&ipvs->sync_buff_lock);
1823 /* increase the module use count */
1824 ip_vs_use_count_inc();
1826 return 0;
1828 outsocket:
1829 sk_release_kernel(sock->sk);
1831 outtinfo:
1832 if (tinfo) {
1833 sk_release_kernel(tinfo->sock->sk);
1834 kfree(tinfo->buf);
1835 kfree(tinfo);
1837 count = id;
1838 while (count-- > 0) {
1839 if (state == IP_VS_STATE_MASTER)
1840 kthread_stop(ipvs->ms[count].master_thread);
1841 else
1842 kthread_stop(array[count]);
1844 kfree(array);
1846 out:
1847 if (!(ipvs->sync_state & IP_VS_STATE_MASTER)) {
1848 kfree(ipvs->ms);
1849 ipvs->ms = NULL;
1851 return result;
1855 int stop_sync_thread(struct net *net, int state)
1857 struct netns_ipvs *ipvs = net_ipvs(net);
1858 struct task_struct **array;
1859 int id;
1860 int retc = -EINVAL;
1862 IP_VS_DBG(7, "%s(): pid %d\n", __func__, task_pid_nr(current));
1864 if (state == IP_VS_STATE_MASTER) {
1865 if (!ipvs->ms)
1866 return -ESRCH;
1869 * The lock synchronizes with sb_queue_tail(), so that we don't
1870 * add sync buffers to the queue, when we are already in
1871 * progress of stopping the master sync daemon.
1874 spin_lock_bh(&ipvs->sync_buff_lock);
1875 spin_lock(&ipvs->sync_lock);
1876 ipvs->sync_state &= ~IP_VS_STATE_MASTER;
1877 spin_unlock(&ipvs->sync_lock);
1878 spin_unlock_bh(&ipvs->sync_buff_lock);
1880 retc = 0;
1881 for (id = ipvs->threads_mask; id >= 0; id--) {
1882 struct ipvs_master_sync_state *ms = &ipvs->ms[id];
1883 int ret;
1885 pr_info("stopping master sync thread %d ...\n",
1886 task_pid_nr(ms->master_thread));
1887 cancel_delayed_work_sync(&ms->master_wakeup_work);
1888 ret = kthread_stop(ms->master_thread);
1889 if (retc >= 0)
1890 retc = ret;
1892 kfree(ipvs->ms);
1893 ipvs->ms = NULL;
1894 } else if (state == IP_VS_STATE_BACKUP) {
1895 if (!ipvs->backup_threads)
1896 return -ESRCH;
1898 ipvs->sync_state &= ~IP_VS_STATE_BACKUP;
1899 array = ipvs->backup_threads;
1900 retc = 0;
1901 for (id = ipvs->threads_mask; id >= 0; id--) {
1902 int ret;
1904 pr_info("stopping backup sync thread %d ...\n",
1905 task_pid_nr(array[id]));
1906 ret = kthread_stop(array[id]);
1907 if (retc >= 0)
1908 retc = ret;
1910 kfree(array);
1911 ipvs->backup_threads = NULL;
1914 /* decrease the module use count */
1915 ip_vs_use_count_dec();
1917 return retc;
1921 * Initialize data struct for each netns
1923 int __net_init ip_vs_sync_net_init(struct net *net)
1925 struct netns_ipvs *ipvs = net_ipvs(net);
1927 __mutex_init(&ipvs->sync_mutex, "ipvs->sync_mutex", &__ipvs_sync_key);
1928 spin_lock_init(&ipvs->sync_lock);
1929 spin_lock_init(&ipvs->sync_buff_lock);
1930 return 0;
1933 void ip_vs_sync_net_cleanup(struct net *net)
1935 int retc;
1936 struct netns_ipvs *ipvs = net_ipvs(net);
1938 mutex_lock(&ipvs->sync_mutex);
1939 retc = stop_sync_thread(net, IP_VS_STATE_MASTER);
1940 if (retc && retc != -ESRCH)
1941 pr_err("Failed to stop Master Daemon\n");
1943 retc = stop_sync_thread(net, IP_VS_STATE_BACKUP);
1944 if (retc && retc != -ESRCH)
1945 pr_err("Failed to stop Backup Daemon\n");
1946 mutex_unlock(&ipvs->sync_mutex);