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
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
26 * Alexandre Cassen : Added master & backup support at a time.
27 * Alexandre Cassen : Added SyncID support for incoming sync
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>
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>
51 #include <linux/sched.h>
53 #include <asm/unaligned.h> /* Used for ntoh_seq and hton_seq */
58 #include <net/ip_vs.h>
60 #define IP_VS_SYNC_GROUP 0xe0000051 /* multicast addr - 224.0.0.81 */
61 #define IP_VS_SYNC_PORT 8848 /* multicast port */
63 #define SYNC_PROTO_VER 1 /* Protocol version in header */
65 static struct lock_class_key __ipvs_sync_key;
67 * IPVS sync connection entry
68 * Version 0, i.e. original version.
70 struct ip_vs_sync_conn_v0 {
73 /* Protocol, addresses and port numbers */
74 __u8 protocol; /* Which protocol (TCP/UDP) */
78 __be32 caddr; /* client address */
79 __be32 vaddr; /* virtual address */
80 __be32 daddr; /* destination address */
82 /* Flags and state transition */
83 __be16 flags; /* status flags */
84 __be16 state; /* state info */
86 /* The sequence options start here */
89 struct ip_vs_sync_conn_options {
90 struct ip_vs_seq in_seq; /* incoming seq. struct */
91 struct ip_vs_seq out_seq; /* outgoing seq. struct */
95 Sync Connection format (sync_conn)
98 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
99 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100 | Type | Protocol | Ver. | Size |
101 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110 | timeout (in sec.) |
111 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113 | IP-Addresses (v4 or v6) |
115 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
117 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
118 | Param. Type | Param. Length | Param. data |
119 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
121 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
122 | | Param Type | Param. Length |
123 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
125 | Last Param data should be padded for 32 bit alignment |
126 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
130 * Type 0, IPv4 sync connection format
132 struct ip_vs_sync_v4 {
134 __u8 protocol; /* Which protocol (TCP/UDP) */
135 __be16 ver_size; /* Version msb 4 bits */
136 /* Flags and state transition */
137 __be32 flags; /* status flags */
138 __be16 state; /* state info */
139 /* Protocol, addresses and port numbers */
143 __be32 fwmark; /* Firewall mark from skb */
144 __be32 timeout; /* cp timeout */
145 __be32 caddr; /* client address */
146 __be32 vaddr; /* virtual address */
147 __be32 daddr; /* destination address */
148 /* The sequence options start here */
149 /* PE data padded to 32bit alignment after seq. options */
152 * Type 2 messages IPv6
154 struct ip_vs_sync_v6 {
156 __u8 protocol; /* Which protocol (TCP/UDP) */
157 __be16 ver_size; /* Version msb 4 bits */
158 /* Flags and state transition */
159 __be32 flags; /* status flags */
160 __be16 state; /* state info */
161 /* Protocol, addresses and port numbers */
165 __be32 fwmark; /* Firewall mark from skb */
166 __be32 timeout; /* cp timeout */
167 struct in6_addr caddr; /* client address */
168 struct in6_addr vaddr; /* virtual address */
169 struct in6_addr daddr; /* destination address */
170 /* The sequence options start here */
171 /* PE data padded to 32bit alignment after seq. options */
174 union ip_vs_sync_conn {
175 struct ip_vs_sync_v4 v4;
176 struct ip_vs_sync_v6 v6;
179 /* Bits in Type field in above */
180 #define STYPE_INET6 0
181 #define STYPE_F_INET6 (1 << STYPE_INET6)
183 #define SVER_SHIFT 12 /* Shift to get version */
184 #define SVER_MASK 0x0fff /* Mask to strip version */
186 #define IPVS_OPT_SEQ_DATA 1
187 #define IPVS_OPT_PE_DATA 2
188 #define IPVS_OPT_PE_NAME 3
189 #define IPVS_OPT_PARAM 7
191 #define IPVS_OPT_F_SEQ_DATA (1 << (IPVS_OPT_SEQ_DATA-1))
192 #define IPVS_OPT_F_PE_DATA (1 << (IPVS_OPT_PE_DATA-1))
193 #define IPVS_OPT_F_PE_NAME (1 << (IPVS_OPT_PE_NAME-1))
194 #define IPVS_OPT_F_PARAM (1 << (IPVS_OPT_PARAM-1))
196 struct ip_vs_sync_thread_data {
197 struct netns_ipvs *ipvs;
203 /* Version 0 definition of packet sizes */
204 #define SIMPLE_CONN_SIZE (sizeof(struct ip_vs_sync_conn_v0))
205 #define FULL_CONN_SIZE \
206 (sizeof(struct ip_vs_sync_conn_v0) + sizeof(struct ip_vs_sync_conn_options))
210 The master mulitcasts messages (Datagrams) to the backup load balancers
211 in the following format.
214 Note, first byte should be Zero, so ver 0 receivers will drop the packet.
217 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
218 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
219 | 0 | SyncID | Size |
220 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
221 | Count Conns | Version | Reserved, set to Zero |
222 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
224 | IPVS Sync Connection (1) |
225 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
229 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
231 | IPVS Sync Connection (n) |
232 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
236 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
237 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
238 | Count Conns | SyncID | Size |
239 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
240 | IPVS Sync Connection (1) |
243 #define SYNC_MESG_HEADER_LEN 4
244 #define MAX_CONNS_PER_SYNCBUFF 255 /* nr_conns in ip_vs_sync_mesg is 8 bit */
246 /* Version 0 header */
247 struct ip_vs_sync_mesg_v0 {
252 /* ip_vs_sync_conn entries start here */
255 /* Version 1 header */
256 struct ip_vs_sync_mesg {
257 __u8 reserved; /* must be zero */
261 __s8 version; /* SYNC_PROTO_VER */
263 /* ip_vs_sync_conn entries start here */
266 union ipvs_sockaddr {
267 struct sockaddr_in in;
268 struct sockaddr_in6 in6;
271 struct ip_vs_sync_buff {
272 struct list_head list;
273 unsigned long firstuse;
275 /* pointers for the message data */
276 struct ip_vs_sync_mesg *mesg;
282 * Copy of struct ip_vs_seq
283 * From unaligned network order to aligned host order
285 static void ntoh_seq(struct ip_vs_seq *no, struct ip_vs_seq *ho)
287 memset(ho, 0, sizeof(*ho));
288 ho->init_seq = get_unaligned_be32(&no->init_seq);
289 ho->delta = get_unaligned_be32(&no->delta);
290 ho->previous_delta = get_unaligned_be32(&no->previous_delta);
294 * Copy of struct ip_vs_seq
295 * From Aligned host order to unaligned network order
297 static void hton_seq(struct ip_vs_seq *ho, struct ip_vs_seq *no)
299 put_unaligned_be32(ho->init_seq, &no->init_seq);
300 put_unaligned_be32(ho->delta, &no->delta);
301 put_unaligned_be32(ho->previous_delta, &no->previous_delta);
304 static inline struct ip_vs_sync_buff *
305 sb_dequeue(struct netns_ipvs *ipvs, struct ipvs_master_sync_state *ms)
307 struct ip_vs_sync_buff *sb;
309 spin_lock_bh(&ipvs->sync_lock);
310 if (list_empty(&ms->sync_queue)) {
312 __set_current_state(TASK_INTERRUPTIBLE);
314 sb = list_entry(ms->sync_queue.next, struct ip_vs_sync_buff,
317 ms->sync_queue_len--;
318 if (!ms->sync_queue_len)
319 ms->sync_queue_delay = 0;
321 spin_unlock_bh(&ipvs->sync_lock);
327 * Create a new sync buffer for Version 1 proto.
329 static inline struct ip_vs_sync_buff *
330 ip_vs_sync_buff_create(struct netns_ipvs *ipvs, unsigned int len)
332 struct ip_vs_sync_buff *sb;
334 if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
337 len = max_t(unsigned int, len + sizeof(struct ip_vs_sync_mesg),
338 ipvs->mcfg.sync_maxlen);
339 sb->mesg = kmalloc(len, GFP_ATOMIC);
344 sb->mesg->reserved = 0; /* old nr_conns i.e. must be zero now */
345 sb->mesg->version = SYNC_PROTO_VER;
346 sb->mesg->syncid = ipvs->mcfg.syncid;
347 sb->mesg->size = htons(sizeof(struct ip_vs_sync_mesg));
348 sb->mesg->nr_conns = 0;
350 sb->head = (unsigned char *)sb->mesg + sizeof(struct ip_vs_sync_mesg);
351 sb->end = (unsigned char *)sb->mesg + len;
353 sb->firstuse = jiffies;
357 static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff *sb)
363 static inline void sb_queue_tail(struct netns_ipvs *ipvs,
364 struct ipvs_master_sync_state *ms)
366 struct ip_vs_sync_buff *sb = ms->sync_buff;
368 spin_lock(&ipvs->sync_lock);
369 if (ipvs->sync_state & IP_VS_STATE_MASTER &&
370 ms->sync_queue_len < sysctl_sync_qlen_max(ipvs)) {
371 if (!ms->sync_queue_len)
372 schedule_delayed_work(&ms->master_wakeup_work,
373 max(IPVS_SYNC_SEND_DELAY, 1));
374 ms->sync_queue_len++;
375 list_add_tail(&sb->list, &ms->sync_queue);
376 if ((++ms->sync_queue_delay) == IPVS_SYNC_WAKEUP_RATE)
377 wake_up_process(ms->master_thread);
379 ip_vs_sync_buff_release(sb);
380 spin_unlock(&ipvs->sync_lock);
384 * Get the current sync buffer if it has been created for more
385 * than the specified time or the specified time is zero.
387 static inline struct ip_vs_sync_buff *
388 get_curr_sync_buff(struct netns_ipvs *ipvs, struct ipvs_master_sync_state *ms,
391 struct ip_vs_sync_buff *sb;
393 spin_lock_bh(&ipvs->sync_buff_lock);
395 if (sb && time_after_eq(jiffies - sb->firstuse, time)) {
396 ms->sync_buff = NULL;
397 __set_current_state(TASK_RUNNING);
400 spin_unlock_bh(&ipvs->sync_buff_lock);
405 select_master_thread_id(struct netns_ipvs *ipvs, struct ip_vs_conn *cp)
407 return ((long) cp >> (1 + ilog2(sizeof(*cp)))) & ipvs->threads_mask;
411 * Create a new sync buffer for Version 0 proto.
413 static inline struct ip_vs_sync_buff *
414 ip_vs_sync_buff_create_v0(struct netns_ipvs *ipvs, unsigned int len)
416 struct ip_vs_sync_buff *sb;
417 struct ip_vs_sync_mesg_v0 *mesg;
419 if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
422 len = max_t(unsigned int, len + sizeof(struct ip_vs_sync_mesg_v0),
423 ipvs->mcfg.sync_maxlen);
424 sb->mesg = kmalloc(len, GFP_ATOMIC);
429 mesg = (struct ip_vs_sync_mesg_v0 *)sb->mesg;
431 mesg->syncid = ipvs->mcfg.syncid;
432 mesg->size = htons(sizeof(struct ip_vs_sync_mesg_v0));
433 sb->head = (unsigned char *)mesg + sizeof(struct ip_vs_sync_mesg_v0);
434 sb->end = (unsigned char *)mesg + len;
435 sb->firstuse = jiffies;
439 /* Check if connection is controlled by persistence */
440 static inline bool in_persistence(struct ip_vs_conn *cp)
442 for (cp = cp->control; cp; cp = cp->control) {
443 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
449 /* Check if conn should be synced.
450 * pkts: conn packets, use sysctl_sync_threshold to avoid packet check
451 * - (1) sync_refresh_period: reduce sync rate. Additionally, retry
452 * sync_retries times with period of sync_refresh_period/8
453 * - (2) if both sync_refresh_period and sync_period are 0 send sync only
454 * for state changes or only once when pkts matches sync_threshold
455 * - (3) templates: rate can be reduced only with sync_refresh_period or
458 static int ip_vs_sync_conn_needed(struct netns_ipvs *ipvs,
459 struct ip_vs_conn *cp, int pkts)
461 unsigned long orig = ACCESS_ONCE(cp->sync_endtime);
462 unsigned long now = jiffies;
463 unsigned long n = (now + cp->timeout) & ~3UL;
464 unsigned int sync_refresh_period;
468 /* Check if we sync in current state */
469 if (unlikely(cp->flags & IP_VS_CONN_F_TEMPLATE))
471 else if (unlikely(sysctl_sync_persist_mode(ipvs) && in_persistence(cp)))
473 else if (likely(cp->protocol == IPPROTO_TCP)) {
474 if (!((1 << cp->state) &
475 ((1 << IP_VS_TCP_S_ESTABLISHED) |
476 (1 << IP_VS_TCP_S_FIN_WAIT) |
477 (1 << IP_VS_TCP_S_CLOSE) |
478 (1 << IP_VS_TCP_S_CLOSE_WAIT) |
479 (1 << IP_VS_TCP_S_TIME_WAIT))))
481 force = cp->state != cp->old_state;
482 if (force && cp->state != IP_VS_TCP_S_ESTABLISHED)
484 } else if (unlikely(cp->protocol == IPPROTO_SCTP)) {
485 if (!((1 << cp->state) &
486 ((1 << IP_VS_SCTP_S_ESTABLISHED) |
487 (1 << IP_VS_SCTP_S_SHUTDOWN_SENT) |
488 (1 << IP_VS_SCTP_S_SHUTDOWN_RECEIVED) |
489 (1 << IP_VS_SCTP_S_SHUTDOWN_ACK_SENT) |
490 (1 << IP_VS_SCTP_S_CLOSED))))
492 force = cp->state != cp->old_state;
493 if (force && cp->state != IP_VS_SCTP_S_ESTABLISHED)
496 /* UDP or another protocol with single state */
500 sync_refresh_period = sysctl_sync_refresh_period(ipvs);
501 if (sync_refresh_period > 0) {
502 long diff = n - orig;
503 long min_diff = max(cp->timeout >> 1, 10UL * HZ);
505 /* Avoid sync if difference is below sync_refresh_period
506 * and below the half timeout.
508 if (abs(diff) < min_t(long, sync_refresh_period, min_diff)) {
509 int retries = orig & 3;
511 if (retries >= sysctl_sync_retries(ipvs))
513 if (time_before(now, orig - cp->timeout +
514 (sync_refresh_period >> 3)))
519 sync_period = sysctl_sync_period(ipvs);
520 if (sync_period > 0) {
521 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE) &&
522 pkts % sync_period != sysctl_sync_threshold(ipvs))
524 } else if (sync_refresh_period <= 0 &&
525 pkts != sysctl_sync_threshold(ipvs))
529 cp->old_state = cp->state;
530 n = cmpxchg(&cp->sync_endtime, orig, n);
531 return n == orig || force;
535 * Version 0 , could be switched in by sys_ctl.
536 * Add an ip_vs_conn information into the current sync_buff.
538 static void ip_vs_sync_conn_v0(struct netns_ipvs *ipvs, struct ip_vs_conn *cp,
541 struct ip_vs_sync_mesg_v0 *m;
542 struct ip_vs_sync_conn_v0 *s;
543 struct ip_vs_sync_buff *buff;
544 struct ipvs_master_sync_state *ms;
548 if (unlikely(cp->af != AF_INET))
550 /* Do not sync ONE PACKET */
551 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
554 if (!ip_vs_sync_conn_needed(ipvs, cp, pkts))
557 spin_lock_bh(&ipvs->sync_buff_lock);
558 if (!(ipvs->sync_state & IP_VS_STATE_MASTER)) {
559 spin_unlock_bh(&ipvs->sync_buff_lock);
563 id = select_master_thread_id(ipvs, cp);
565 buff = ms->sync_buff;
566 len = (cp->flags & IP_VS_CONN_F_SEQ_MASK) ? FULL_CONN_SIZE :
569 m = (struct ip_vs_sync_mesg_v0 *) buff->mesg;
570 /* Send buffer if it is for v1 */
571 if (buff->head + len > buff->end || !m->nr_conns) {
572 sb_queue_tail(ipvs, ms);
573 ms->sync_buff = NULL;
578 buff = ip_vs_sync_buff_create_v0(ipvs, len);
580 spin_unlock_bh(&ipvs->sync_buff_lock);
581 pr_err("ip_vs_sync_buff_create failed.\n");
584 ms->sync_buff = buff;
587 m = (struct ip_vs_sync_mesg_v0 *) buff->mesg;
588 s = (struct ip_vs_sync_conn_v0 *) buff->head;
592 s->protocol = cp->protocol;
593 s->cport = cp->cport;
594 s->vport = cp->vport;
595 s->dport = cp->dport;
596 s->caddr = cp->caddr.ip;
597 s->vaddr = cp->vaddr.ip;
598 s->daddr = cp->daddr.ip;
599 s->flags = htons(cp->flags & ~IP_VS_CONN_F_HASHED);
600 s->state = htons(cp->state);
601 if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
602 struct ip_vs_sync_conn_options *opt =
603 (struct ip_vs_sync_conn_options *)&s[1];
604 memcpy(opt, &cp->in_seq, sizeof(*opt));
608 m->size = htons(ntohs(m->size) + len);
610 spin_unlock_bh(&ipvs->sync_buff_lock);
612 /* synchronize its controller if it has */
615 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
616 pkts = atomic_add_return(1, &cp->in_pkts);
618 pkts = sysctl_sync_threshold(ipvs);
619 ip_vs_sync_conn(ipvs, cp, pkts);
624 * Add an ip_vs_conn information into the current sync_buff.
625 * Called by ip_vs_in.
626 * Sending Version 1 messages
628 void ip_vs_sync_conn(struct netns_ipvs *ipvs, struct ip_vs_conn *cp, int pkts)
630 struct ip_vs_sync_mesg *m;
631 union ip_vs_sync_conn *s;
632 struct ip_vs_sync_buff *buff;
633 struct ipvs_master_sync_state *ms;
636 unsigned int len, pe_name_len, pad;
638 /* Handle old version of the protocol */
639 if (sysctl_sync_ver(ipvs) == 0) {
640 ip_vs_sync_conn_v0(ipvs, cp, pkts);
643 /* Do not sync ONE PACKET */
644 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
647 if (!ip_vs_sync_conn_needed(ipvs, cp, pkts))
652 if (cp->pe_data_len) {
653 if (!cp->pe_data || !cp->dest) {
654 IP_VS_ERR_RL("SYNC, connection pe_data invalid\n");
657 pe_name_len = strnlen(cp->pe->name, IP_VS_PENAME_MAXLEN);
660 spin_lock_bh(&ipvs->sync_buff_lock);
661 if (!(ipvs->sync_state & IP_VS_STATE_MASTER)) {
662 spin_unlock_bh(&ipvs->sync_buff_lock);
666 id = select_master_thread_id(ipvs, cp);
669 #ifdef CONFIG_IP_VS_IPV6
670 if (cp->af == AF_INET6)
671 len = sizeof(struct ip_vs_sync_v6);
674 len = sizeof(struct ip_vs_sync_v4);
676 if (cp->flags & IP_VS_CONN_F_SEQ_MASK)
677 len += sizeof(struct ip_vs_sync_conn_options) + 2;
680 len += cp->pe_data_len + 2; /* + Param hdr field */
682 len += pe_name_len + 2;
684 /* check if there is a space for this one */
686 buff = ms->sync_buff;
689 pad = (4 - (size_t) buff->head) & 3;
690 /* Send buffer if it is for v0 */
691 if (buff->head + len + pad > buff->end || m->reserved) {
692 sb_queue_tail(ipvs, ms);
693 ms->sync_buff = NULL;
700 buff = ip_vs_sync_buff_create(ipvs, len);
702 spin_unlock_bh(&ipvs->sync_buff_lock);
703 pr_err("ip_vs_sync_buff_create failed.\n");
706 ms->sync_buff = buff;
711 buff->head += pad + len;
712 m->size = htons(ntohs(m->size) + pad + len);
713 /* Add ev. padding from prev. sync_conn */
717 s = (union ip_vs_sync_conn *)p;
719 /* Set message type & copy members */
720 s->v4.type = (cp->af == AF_INET6 ? STYPE_F_INET6 : 0);
721 s->v4.ver_size = htons(len & SVER_MASK); /* Version 0 */
722 s->v4.flags = htonl(cp->flags & ~IP_VS_CONN_F_HASHED);
723 s->v4.state = htons(cp->state);
724 s->v4.protocol = cp->protocol;
725 s->v4.cport = cp->cport;
726 s->v4.vport = cp->vport;
727 s->v4.dport = cp->dport;
728 s->v4.fwmark = htonl(cp->fwmark);
729 s->v4.timeout = htonl(cp->timeout / HZ);
732 #ifdef CONFIG_IP_VS_IPV6
733 if (cp->af == AF_INET6) {
734 p += sizeof(struct ip_vs_sync_v6);
735 s->v6.caddr = cp->caddr.in6;
736 s->v6.vaddr = cp->vaddr.in6;
737 s->v6.daddr = cp->daddr.in6;
741 p += sizeof(struct ip_vs_sync_v4); /* options ptr */
742 s->v4.caddr = cp->caddr.ip;
743 s->v4.vaddr = cp->vaddr.ip;
744 s->v4.daddr = cp->daddr.ip;
746 if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
747 *(p++) = IPVS_OPT_SEQ_DATA;
748 *(p++) = sizeof(struct ip_vs_sync_conn_options);
749 hton_seq((struct ip_vs_seq *)p, &cp->in_seq);
750 p += sizeof(struct ip_vs_seq);
751 hton_seq((struct ip_vs_seq *)p, &cp->out_seq);
752 p += sizeof(struct ip_vs_seq);
755 if (cp->pe_data_len && cp->pe_data) {
756 *(p++) = IPVS_OPT_PE_DATA;
757 *(p++) = cp->pe_data_len;
758 memcpy(p, cp->pe_data, cp->pe_data_len);
759 p += cp->pe_data_len;
762 *(p++) = IPVS_OPT_PE_NAME;
763 *(p++) = pe_name_len;
764 memcpy(p, cp->pe->name, pe_name_len);
769 spin_unlock_bh(&ipvs->sync_buff_lock);
772 /* synchronize its controller if it has */
776 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
777 pkts = atomic_add_return(1, &cp->in_pkts);
779 pkts = sysctl_sync_threshold(ipvs);
784 * fill_param used by version 1
787 ip_vs_conn_fill_param_sync(struct netns_ipvs *ipvs, int af, union ip_vs_sync_conn *sc,
788 struct ip_vs_conn_param *p,
789 __u8 *pe_data, unsigned int pe_data_len,
790 __u8 *pe_name, unsigned int pe_name_len)
792 #ifdef CONFIG_IP_VS_IPV6
794 ip_vs_conn_fill_param(ipvs, af, sc->v6.protocol,
795 (const union nf_inet_addr *)&sc->v6.caddr,
797 (const union nf_inet_addr *)&sc->v6.vaddr,
801 ip_vs_conn_fill_param(ipvs, af, sc->v4.protocol,
802 (const union nf_inet_addr *)&sc->v4.caddr,
804 (const union nf_inet_addr *)&sc->v4.vaddr,
809 char buff[IP_VS_PENAME_MAXLEN+1];
811 memcpy(buff, pe_name, pe_name_len);
813 p->pe = __ip_vs_pe_getbyname(buff);
815 IP_VS_DBG(3, "BACKUP, no %s engine found/loaded\n",
820 IP_VS_ERR_RL("BACKUP, Invalid PE parameters\n");
824 p->pe_data = kmemdup(pe_data, pe_data_len, GFP_ATOMIC);
826 module_put(p->pe->module);
829 p->pe_data_len = pe_data_len;
835 * Connection Add / Update.
836 * Common for version 0 and 1 reception of backup sync_conns.
840 static void ip_vs_proc_conn(struct netns_ipvs *ipvs, struct ip_vs_conn_param *param,
841 unsigned int flags, unsigned int state,
842 unsigned int protocol, unsigned int type,
843 const union nf_inet_addr *daddr, __be16 dport,
844 unsigned long timeout, __u32 fwmark,
845 struct ip_vs_sync_conn_options *opt)
847 struct ip_vs_dest *dest;
848 struct ip_vs_conn *cp;
850 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
851 cp = ip_vs_conn_in_get(param);
852 if (cp && ((cp->dport != dport) ||
853 !ip_vs_addr_equal(cp->daf, &cp->daddr, daddr))) {
854 if (!(flags & IP_VS_CONN_F_INACTIVE)) {
855 ip_vs_conn_expire_now(cp);
856 __ip_vs_conn_put(cp);
859 /* This is the expiration message for the
860 * connection that was already replaced, so we
863 __ip_vs_conn_put(cp);
864 kfree(param->pe_data);
869 cp = ip_vs_ct_in_get(param);
874 kfree(param->pe_data);
877 spin_lock_bh(&cp->lock);
878 if ((cp->flags ^ flags) & IP_VS_CONN_F_INACTIVE &&
879 !(flags & IP_VS_CONN_F_TEMPLATE) && dest) {
880 if (flags & IP_VS_CONN_F_INACTIVE) {
881 atomic_dec(&dest->activeconns);
882 atomic_inc(&dest->inactconns);
884 atomic_inc(&dest->activeconns);
885 atomic_dec(&dest->inactconns);
888 flags &= IP_VS_CONN_F_BACKUP_UPD_MASK;
889 flags |= cp->flags & ~IP_VS_CONN_F_BACKUP_UPD_MASK;
891 spin_unlock_bh(&cp->lock);
893 ip_vs_try_bind_dest(cp);
896 * Find the appropriate destination for the connection.
897 * If it is not found the connection will remain unbound
901 /* This function is only invoked by the synchronization
902 * code. We do not currently support heterogeneous pools
903 * with synchronization, so we can make the assumption that
904 * the svc_af is the same as the dest_af
906 dest = ip_vs_find_dest(ipvs, type, type, daddr, dport,
907 param->vaddr, param->vport, protocol,
910 cp = ip_vs_conn_new(param, type, daddr, dport, flags, dest,
914 kfree(param->pe_data);
915 IP_VS_DBG(2, "BACKUP, add new conn. failed\n");
918 if (!(flags & IP_VS_CONN_F_TEMPLATE))
919 kfree(param->pe_data);
923 cp->in_seq = opt->in_seq;
924 cp->out_seq = opt->out_seq;
926 atomic_set(&cp->in_pkts, sysctl_sync_threshold(ipvs));
928 cp->old_state = cp->state;
930 * For Ver 0 messages style
931 * - Not possible to recover the right timeout for templates
932 * - can not find the right fwmark
933 * virtual service. If needed, we can do it for
934 * non-fwmark persistent services.
935 * Ver 1 messages style.
939 if (timeout > MAX_SCHEDULE_TIMEOUT / HZ)
940 timeout = MAX_SCHEDULE_TIMEOUT / HZ;
941 cp->timeout = timeout*HZ;
943 struct ip_vs_proto_data *pd;
945 pd = ip_vs_proto_data_get(ipvs, protocol);
946 if (!(flags & IP_VS_CONN_F_TEMPLATE) && pd && pd->timeout_table)
947 cp->timeout = pd->timeout_table[state];
949 cp->timeout = (3*60*HZ);
955 * Process received multicast message for Version 0
957 static void ip_vs_process_message_v0(struct netns_ipvs *ipvs, const char *buffer,
960 struct ip_vs_sync_mesg_v0 *m = (struct ip_vs_sync_mesg_v0 *)buffer;
961 struct ip_vs_sync_conn_v0 *s;
962 struct ip_vs_sync_conn_options *opt;
963 struct ip_vs_protocol *pp;
964 struct ip_vs_conn_param param;
968 p = (char *)buffer + sizeof(struct ip_vs_sync_mesg_v0);
969 for (i=0; i<m->nr_conns; i++) {
970 unsigned int flags, state;
972 if (p + SIMPLE_CONN_SIZE > buffer+buflen) {
973 IP_VS_ERR_RL("BACKUP v0, bogus conn\n");
976 s = (struct ip_vs_sync_conn_v0 *) p;
977 flags = ntohs(s->flags) | IP_VS_CONN_F_SYNC;
978 flags &= ~IP_VS_CONN_F_HASHED;
979 if (flags & IP_VS_CONN_F_SEQ_MASK) {
980 opt = (struct ip_vs_sync_conn_options *)&s[1];
982 if (p > buffer+buflen) {
983 IP_VS_ERR_RL("BACKUP v0, Dropping buffer bogus conn options\n");
988 p += SIMPLE_CONN_SIZE;
991 state = ntohs(s->state);
992 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
993 pp = ip_vs_proto_get(s->protocol);
995 IP_VS_DBG(2, "BACKUP v0, Unsupported protocol %u\n",
999 if (state >= pp->num_states) {
1000 IP_VS_DBG(2, "BACKUP v0, Invalid %s state %u\n",
1005 /* protocol in templates is not used for state/timeout */
1007 IP_VS_DBG(2, "BACKUP v0, Invalid template state %u\n",
1013 ip_vs_conn_fill_param(ipvs, AF_INET, s->protocol,
1014 (const union nf_inet_addr *)&s->caddr,
1016 (const union nf_inet_addr *)&s->vaddr,
1019 /* Send timeout as Zero */
1020 ip_vs_proc_conn(ipvs, ¶m, flags, state, s->protocol, AF_INET,
1021 (union nf_inet_addr *)&s->daddr, s->dport,
1029 static inline int ip_vs_proc_seqopt(__u8 *p, unsigned int plen,
1031 struct ip_vs_sync_conn_options *opt)
1033 struct ip_vs_sync_conn_options *topt;
1035 topt = (struct ip_vs_sync_conn_options *)p;
1037 if (plen != sizeof(struct ip_vs_sync_conn_options)) {
1038 IP_VS_DBG(2, "BACKUP, bogus conn options length\n");
1041 if (*opt_flags & IPVS_OPT_F_SEQ_DATA) {
1042 IP_VS_DBG(2, "BACKUP, conn options found twice\n");
1045 ntoh_seq(&topt->in_seq, &opt->in_seq);
1046 ntoh_seq(&topt->out_seq, &opt->out_seq);
1047 *opt_flags |= IPVS_OPT_F_SEQ_DATA;
1051 static int ip_vs_proc_str(__u8 *p, unsigned int plen, unsigned int *data_len,
1052 __u8 **data, unsigned int maxlen,
1053 __u32 *opt_flags, __u32 flag)
1055 if (plen > maxlen) {
1056 IP_VS_DBG(2, "BACKUP, bogus par.data len > %d\n", maxlen);
1059 if (*opt_flags & flag) {
1060 IP_VS_DBG(2, "BACKUP, Par.data found twice 0x%x\n", flag);
1069 * Process a Version 1 sync. connection
1071 static inline int ip_vs_proc_sync_conn(struct netns_ipvs *ipvs, __u8 *p, __u8 *msg_end)
1073 struct ip_vs_sync_conn_options opt;
1074 union ip_vs_sync_conn *s;
1075 struct ip_vs_protocol *pp;
1076 struct ip_vs_conn_param param;
1078 unsigned int af, state, pe_data_len=0, pe_name_len=0;
1079 __u8 *pe_data=NULL, *pe_name=NULL;
1083 s = (union ip_vs_sync_conn *) p;
1085 if (s->v6.type & STYPE_F_INET6) {
1086 #ifdef CONFIG_IP_VS_IPV6
1088 p += sizeof(struct ip_vs_sync_v6);
1090 IP_VS_DBG(3,"BACKUP, IPv6 msg received, and IPVS is not compiled for IPv6\n");
1094 } else if (!s->v4.type) {
1096 p += sizeof(struct ip_vs_sync_v4);
1103 /* Process optional params check Type & Len. */
1104 while (p < msg_end) {
1113 if (!plen || ((p + plen) > msg_end))
1115 /* Handle seq option p = param data */
1116 switch (ptype & ~IPVS_OPT_F_PARAM) {
1117 case IPVS_OPT_SEQ_DATA:
1118 if (ip_vs_proc_seqopt(p, plen, &opt_flags, &opt))
1122 case IPVS_OPT_PE_DATA:
1123 if (ip_vs_proc_str(p, plen, &pe_data_len, &pe_data,
1124 IP_VS_PEDATA_MAXLEN, &opt_flags,
1125 IPVS_OPT_F_PE_DATA))
1129 case IPVS_OPT_PE_NAME:
1130 if (ip_vs_proc_str(p, plen,&pe_name_len, &pe_name,
1131 IP_VS_PENAME_MAXLEN, &opt_flags,
1132 IPVS_OPT_F_PE_NAME))
1137 /* Param data mandatory ? */
1138 if (!(ptype & IPVS_OPT_F_PARAM)) {
1139 IP_VS_DBG(3, "BACKUP, Unknown mandatory param %d found\n",
1140 ptype & ~IPVS_OPT_F_PARAM);
1145 p += plen; /* Next option */
1148 /* Get flags and Mask off unsupported */
1149 flags = ntohl(s->v4.flags) & IP_VS_CONN_F_BACKUP_MASK;
1150 flags |= IP_VS_CONN_F_SYNC;
1151 state = ntohs(s->v4.state);
1153 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
1154 pp = ip_vs_proto_get(s->v4.protocol);
1156 IP_VS_DBG(3,"BACKUP, Unsupported protocol %u\n",
1161 if (state >= pp->num_states) {
1162 IP_VS_DBG(3, "BACKUP, Invalid %s state %u\n",
1168 /* protocol in templates is not used for state/timeout */
1170 IP_VS_DBG(3, "BACKUP, Invalid template state %u\n",
1175 if (ip_vs_conn_fill_param_sync(ipvs, af, s, ¶m, pe_data,
1176 pe_data_len, pe_name, pe_name_len)) {
1180 /* If only IPv4, just silent skip IPv6 */
1182 ip_vs_proc_conn(ipvs, ¶m, flags, state, s->v4.protocol, af,
1183 (union nf_inet_addr *)&s->v4.daddr, s->v4.dport,
1184 ntohl(s->v4.timeout), ntohl(s->v4.fwmark),
1185 (opt_flags & IPVS_OPT_F_SEQ_DATA ? &opt : NULL)
1187 #ifdef CONFIG_IP_VS_IPV6
1189 ip_vs_proc_conn(ipvs, ¶m, flags, state, s->v6.protocol, af,
1190 (union nf_inet_addr *)&s->v6.daddr, s->v6.dport,
1191 ntohl(s->v6.timeout), ntohl(s->v6.fwmark),
1192 (opt_flags & IPVS_OPT_F_SEQ_DATA ? &opt : NULL)
1195 ip_vs_pe_put(param.pe);
1199 IP_VS_DBG(2, "BACKUP, Single msg dropped err:%d\n", retc);
1204 * Process received multicast message and create the corresponding
1205 * ip_vs_conn entries.
1206 * Handles Version 0 & 1
1208 static void ip_vs_process_message(struct netns_ipvs *ipvs, __u8 *buffer,
1209 const size_t buflen)
1211 struct ip_vs_sync_mesg *m2 = (struct ip_vs_sync_mesg *)buffer;
1215 if (buflen < sizeof(struct ip_vs_sync_mesg_v0)) {
1216 IP_VS_DBG(2, "BACKUP, message header too short\n");
1220 if (buflen != ntohs(m2->size)) {
1221 IP_VS_DBG(2, "BACKUP, bogus message size\n");
1224 /* SyncID sanity check */
1225 if (ipvs->bcfg.syncid != 0 && m2->syncid != ipvs->bcfg.syncid) {
1226 IP_VS_DBG(7, "BACKUP, Ignoring syncid = %d\n", m2->syncid);
1229 /* Handle version 1 message */
1230 if ((m2->version == SYNC_PROTO_VER) && (m2->reserved == 0)
1231 && (m2->spare == 0)) {
1233 msg_end = buffer + sizeof(struct ip_vs_sync_mesg);
1234 nr_conns = m2->nr_conns;
1236 for (i=0; i<nr_conns; i++) {
1237 union ip_vs_sync_conn *s;
1242 if (p + sizeof(s->v4) > buffer+buflen) {
1243 IP_VS_ERR_RL("BACKUP, Dropping buffer, to small\n");
1246 s = (union ip_vs_sync_conn *)p;
1247 size = ntohs(s->v4.ver_size) & SVER_MASK;
1249 /* Basic sanity checks */
1250 if (msg_end > buffer+buflen) {
1251 IP_VS_ERR_RL("BACKUP, Dropping buffer, msg > buffer\n");
1254 if (ntohs(s->v4.ver_size) >> SVER_SHIFT) {
1255 IP_VS_ERR_RL("BACKUP, Dropping buffer, Unknown version %d\n",
1256 ntohs(s->v4.ver_size) >> SVER_SHIFT);
1259 /* Process a single sync_conn */
1260 retc = ip_vs_proc_sync_conn(ipvs, p, msg_end);
1262 IP_VS_ERR_RL("BACKUP, Dropping buffer, Err: %d in decoding\n",
1266 /* Make sure we have 32 bit alignment */
1267 msg_end = p + ((size + 3) & ~3);
1270 /* Old type of message */
1271 ip_vs_process_message_v0(ipvs, buffer, buflen);
1278 * Setup sndbuf (mode=1) or rcvbuf (mode=0)
1280 static void set_sock_size(struct sock *sk, int mode, int val)
1282 /* setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &val, sizeof(val)); */
1283 /* setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)); */
1286 val = clamp_t(int, val, (SOCK_MIN_SNDBUF + 1) / 2,
1288 sk->sk_sndbuf = val * 2;
1289 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
1291 val = clamp_t(int, val, (SOCK_MIN_RCVBUF + 1) / 2,
1293 sk->sk_rcvbuf = val * 2;
1294 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
1300 * Setup loopback of outgoing multicasts on a sending socket
1302 static void set_mcast_loop(struct sock *sk, u_char loop)
1304 struct inet_sock *inet = inet_sk(sk);
1306 /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
1308 inet->mc_loop = loop ? 1 : 0;
1309 #ifdef CONFIG_IP_VS_IPV6
1310 if (sk->sk_family == AF_INET6) {
1311 struct ipv6_pinfo *np = inet6_sk(sk);
1313 /* IPV6_MULTICAST_LOOP */
1314 np->mc_loop = loop ? 1 : 0;
1321 * Specify TTL for outgoing multicasts on a sending socket
1323 static void set_mcast_ttl(struct sock *sk, u_char ttl)
1325 struct inet_sock *inet = inet_sk(sk);
1327 /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
1330 #ifdef CONFIG_IP_VS_IPV6
1331 if (sk->sk_family == AF_INET6) {
1332 struct ipv6_pinfo *np = inet6_sk(sk);
1334 /* IPV6_MULTICAST_HOPS */
1335 np->mcast_hops = ttl;
1341 /* Control fragmentation of messages */
1342 static void set_mcast_pmtudisc(struct sock *sk, int val)
1344 struct inet_sock *inet = inet_sk(sk);
1346 /* setsockopt(sock, SOL_IP, IP_MTU_DISCOVER, &val, sizeof(val)); */
1348 inet->pmtudisc = val;
1349 #ifdef CONFIG_IP_VS_IPV6
1350 if (sk->sk_family == AF_INET6) {
1351 struct ipv6_pinfo *np = inet6_sk(sk);
1353 /* IPV6_MTU_DISCOVER */
1361 * Specifiy default interface for outgoing multicasts
1363 static int set_mcast_if(struct sock *sk, struct net_device *dev)
1365 struct inet_sock *inet = inet_sk(sk);
1367 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
1371 inet->mc_index = dev->ifindex;
1372 /* inet->mc_addr = 0; */
1373 #ifdef CONFIG_IP_VS_IPV6
1374 if (sk->sk_family == AF_INET6) {
1375 struct ipv6_pinfo *np = inet6_sk(sk);
1377 /* IPV6_MULTICAST_IF */
1378 np->mcast_oif = dev->ifindex;
1388 * Join a multicast group.
1389 * the group is specified by a class D multicast address 224.0.0.0/8
1390 * in the in_addr structure passed in as a parameter.
1393 join_mcast_group(struct sock *sk, struct in_addr *addr, struct net_device *dev)
1395 struct ip_mreqn mreq;
1398 memset(&mreq, 0, sizeof(mreq));
1399 memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
1401 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
1404 mreq.imr_ifindex = dev->ifindex;
1407 ret = ip_mc_join_group(sk, &mreq);
1413 #ifdef CONFIG_IP_VS_IPV6
1414 static int join_mcast_group6(struct sock *sk, struct in6_addr *addr,
1415 struct net_device *dev)
1419 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
1423 ret = ipv6_sock_mc_join(sk, dev->ifindex, addr);
1430 static int bind_mcastif_addr(struct socket *sock, struct net_device *dev)
1433 struct sockaddr_in sin;
1435 addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
1437 pr_err("You probably need to specify IP address on "
1438 "multicast interface.\n");
1440 IP_VS_DBG(7, "binding socket with (%s) %pI4\n",
1443 /* Now bind the socket with the address of multicast interface */
1444 sin.sin_family = AF_INET;
1445 sin.sin_addr.s_addr = addr;
1448 return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
1451 static void get_mcast_sockaddr(union ipvs_sockaddr *sa, int *salen,
1452 struct ipvs_sync_daemon_cfg *c, int id)
1454 if (AF_INET6 == c->mcast_af) {
1455 sa->in6 = (struct sockaddr_in6) {
1456 .sin6_family = AF_INET6,
1457 .sin6_port = htons(c->mcast_port + id),
1459 sa->in6.sin6_addr = c->mcast_group.in6;
1460 *salen = sizeof(sa->in6);
1462 sa->in = (struct sockaddr_in) {
1463 .sin_family = AF_INET,
1464 .sin_port = htons(c->mcast_port + id),
1466 sa->in.sin_addr = c->mcast_group.in;
1467 *salen = sizeof(sa->in);
1472 * Set up sending multicast socket over UDP
1474 static int make_send_sock(struct netns_ipvs *ipvs, int id,
1475 struct net_device *dev, struct socket **sock_ret)
1477 /* multicast addr */
1478 union ipvs_sockaddr mcast_addr;
1479 struct socket *sock;
1482 /* First create a socket */
1483 result = sock_create_kern(ipvs->net, ipvs->mcfg.mcast_af, SOCK_DGRAM,
1484 IPPROTO_UDP, &sock);
1486 pr_err("Error during creation of socket; terminating\n");
1490 result = set_mcast_if(sock->sk, dev);
1492 pr_err("Error setting outbound mcast interface\n");
1496 set_mcast_loop(sock->sk, 0);
1497 set_mcast_ttl(sock->sk, ipvs->mcfg.mcast_ttl);
1498 /* Allow fragmentation if MTU changes */
1499 set_mcast_pmtudisc(sock->sk, IP_PMTUDISC_DONT);
1500 result = sysctl_sync_sock_size(ipvs);
1502 set_sock_size(sock->sk, 1, result);
1504 if (AF_INET == ipvs->mcfg.mcast_af)
1505 result = bind_mcastif_addr(sock, dev);
1509 pr_err("Error binding address of the mcast interface\n");
1513 get_mcast_sockaddr(&mcast_addr, &salen, &ipvs->mcfg, id);
1514 result = sock->ops->connect(sock, (struct sockaddr *) &mcast_addr,
1517 pr_err("Error connecting to the multicast addr\n");
1529 * Set up receiving multicast socket over UDP
1531 static int make_receive_sock(struct netns_ipvs *ipvs, int id,
1532 struct net_device *dev, struct socket **sock_ret)
1534 /* multicast addr */
1535 union ipvs_sockaddr mcast_addr;
1536 struct socket *sock;
1539 /* First create a socket */
1540 result = sock_create_kern(ipvs->net, ipvs->bcfg.mcast_af, SOCK_DGRAM,
1541 IPPROTO_UDP, &sock);
1543 pr_err("Error during creation of socket; terminating\n");
1547 /* it is equivalent to the REUSEADDR option in user-space */
1548 sock->sk->sk_reuse = SK_CAN_REUSE;
1549 result = sysctl_sync_sock_size(ipvs);
1551 set_sock_size(sock->sk, 0, result);
1553 get_mcast_sockaddr(&mcast_addr, &salen, &ipvs->bcfg, id);
1554 sock->sk->sk_bound_dev_if = dev->ifindex;
1555 result = sock->ops->bind(sock, (struct sockaddr *)&mcast_addr, salen);
1557 pr_err("Error binding to the multicast addr\n");
1561 /* join the multicast group */
1562 #ifdef CONFIG_IP_VS_IPV6
1563 if (ipvs->bcfg.mcast_af == AF_INET6)
1564 result = join_mcast_group6(sock->sk, &mcast_addr.in6.sin6_addr,
1568 result = join_mcast_group(sock->sk, &mcast_addr.in.sin_addr,
1571 pr_err("Error joining to the multicast group\n");
1583 ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
1585 struct msghdr msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
1590 iov.iov_base = (void *)buffer;
1591 iov.iov_len = length;
1593 len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
1600 ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
1605 msize = ntohs(msg->size);
1607 ret = ip_vs_send_async(sock, (char *)msg, msize);
1608 if (ret >= 0 || ret == -EAGAIN)
1610 pr_err("ip_vs_send_async error %d\n", ret);
1615 ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
1617 struct msghdr msg = {NULL,};
1623 /* Receive a packet */
1624 iov.iov_base = buffer;
1625 iov.iov_len = (size_t)buflen;
1627 len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, MSG_DONTWAIT);
1636 /* Wakeup the master thread for sending */
1637 static void master_wakeup_work_handler(struct work_struct *work)
1639 struct ipvs_master_sync_state *ms =
1640 container_of(work, struct ipvs_master_sync_state,
1641 master_wakeup_work.work);
1642 struct netns_ipvs *ipvs = ms->ipvs;
1644 spin_lock_bh(&ipvs->sync_lock);
1645 if (ms->sync_queue_len &&
1646 ms->sync_queue_delay < IPVS_SYNC_WAKEUP_RATE) {
1647 ms->sync_queue_delay = IPVS_SYNC_WAKEUP_RATE;
1648 wake_up_process(ms->master_thread);
1650 spin_unlock_bh(&ipvs->sync_lock);
1653 /* Get next buffer to send */
1654 static inline struct ip_vs_sync_buff *
1655 next_sync_buff(struct netns_ipvs *ipvs, struct ipvs_master_sync_state *ms)
1657 struct ip_vs_sync_buff *sb;
1659 sb = sb_dequeue(ipvs, ms);
1662 /* Do not delay entries in buffer for more than 2 seconds */
1663 return get_curr_sync_buff(ipvs, ms, IPVS_SYNC_FLUSH_TIME);
1666 static int sync_thread_master(void *data)
1668 struct ip_vs_sync_thread_data *tinfo = data;
1669 struct netns_ipvs *ipvs = tinfo->ipvs;
1670 struct ipvs_master_sync_state *ms = &ipvs->ms[tinfo->id];
1671 struct sock *sk = tinfo->sock->sk;
1672 struct ip_vs_sync_buff *sb;
1674 pr_info("sync thread started: state = MASTER, mcast_ifn = %s, "
1675 "syncid = %d, id = %d\n",
1676 ipvs->mcfg.mcast_ifn, ipvs->mcfg.syncid, tinfo->id);
1679 sb = next_sync_buff(ipvs, ms);
1680 if (unlikely(kthread_should_stop()))
1683 schedule_timeout(IPVS_SYNC_CHECK_PERIOD);
1686 while (ip_vs_send_sync_msg(tinfo->sock, sb->mesg) < 0) {
1687 /* (Ab)use interruptible sleep to avoid increasing
1690 __wait_event_interruptible(*sk_sleep(sk),
1691 sock_writeable(sk) ||
1692 kthread_should_stop());
1693 if (unlikely(kthread_should_stop()))
1696 ip_vs_sync_buff_release(sb);
1700 __set_current_state(TASK_RUNNING);
1702 ip_vs_sync_buff_release(sb);
1704 /* clean up the sync_buff queue */
1705 while ((sb = sb_dequeue(ipvs, ms)))
1706 ip_vs_sync_buff_release(sb);
1707 __set_current_state(TASK_RUNNING);
1709 /* clean up the current sync_buff */
1710 sb = get_curr_sync_buff(ipvs, ms, 0);
1712 ip_vs_sync_buff_release(sb);
1714 /* release the sending multicast socket */
1715 sock_release(tinfo->sock);
1722 static int sync_thread_backup(void *data)
1724 struct ip_vs_sync_thread_data *tinfo = data;
1725 struct netns_ipvs *ipvs = tinfo->ipvs;
1728 pr_info("sync thread started: state = BACKUP, mcast_ifn = %s, "
1729 "syncid = %d, id = %d\n",
1730 ipvs->bcfg.mcast_ifn, ipvs->bcfg.syncid, tinfo->id);
1732 while (!kthread_should_stop()) {
1733 wait_event_interruptible(*sk_sleep(tinfo->sock->sk),
1734 !skb_queue_empty(&tinfo->sock->sk->sk_receive_queue)
1735 || kthread_should_stop());
1737 /* do we have data now? */
1738 while (!skb_queue_empty(&(tinfo->sock->sk->sk_receive_queue))) {
1739 len = ip_vs_receive(tinfo->sock, tinfo->buf,
1740 ipvs->bcfg.sync_maxlen);
1743 pr_err("receiving message error\n");
1747 ip_vs_process_message(ipvs, tinfo->buf, len);
1751 /* release the sending multicast socket */
1752 sock_release(tinfo->sock);
1760 int start_sync_thread(struct netns_ipvs *ipvs, struct ipvs_sync_daemon_cfg *c,
1763 struct ip_vs_sync_thread_data *tinfo = NULL;
1764 struct task_struct **array = NULL, *task;
1765 struct net_device *dev;
1767 int (*threadfn)(void *data);
1768 int id = 0, count, hlen;
1769 int result = -ENOMEM;
1772 IP_VS_DBG(7, "%s(): pid %d\n", __func__, task_pid_nr(current));
1773 IP_VS_DBG(7, "Each ip_vs_sync_conn entry needs %Zd bytes\n",
1774 sizeof(struct ip_vs_sync_conn_v0));
1776 /* Do not hold one mutex and then to block on another */
1779 if (mutex_trylock(&ipvs->sync_mutex))
1782 mutex_lock(&ipvs->sync_mutex);
1785 mutex_unlock(&ipvs->sync_mutex);
1788 if (!ipvs->sync_state) {
1789 count = clamp(sysctl_sync_ports(ipvs), 1, IPVS_SYNC_PORTS_MAX);
1790 ipvs->threads_mask = count - 1;
1792 count = ipvs->threads_mask + 1;
1794 if (c->mcast_af == AF_UNSPEC) {
1795 c->mcast_af = AF_INET;
1796 c->mcast_group.ip = cpu_to_be32(IP_VS_SYNC_GROUP);
1799 c->mcast_port = IP_VS_SYNC_PORT;
1803 dev = __dev_get_by_name(ipvs->net, c->mcast_ifn);
1805 pr_err("Unknown mcast interface: %s\n", c->mcast_ifn);
1809 hlen = (AF_INET6 == c->mcast_af) ?
1810 sizeof(struct ipv6hdr) + sizeof(struct udphdr) :
1811 sizeof(struct iphdr) + sizeof(struct udphdr);
1812 mtu = (state == IP_VS_STATE_BACKUP) ?
1813 clamp(dev->mtu, 1500U, 65535U) : 1500U;
1814 min_mtu = (state == IP_VS_STATE_BACKUP) ? 1024 : 1;
1817 c->sync_maxlen = clamp_t(unsigned int,
1818 c->sync_maxlen, min_mtu,
1821 c->sync_maxlen = mtu - hlen;
1823 if (state == IP_VS_STATE_MASTER) {
1829 name = "ipvs-m:%d:%d";
1830 threadfn = sync_thread_master;
1831 } else if (state == IP_VS_STATE_BACKUP) {
1833 if (ipvs->backup_threads)
1837 name = "ipvs-b:%d:%d";
1838 threadfn = sync_thread_backup;
1844 if (state == IP_VS_STATE_MASTER) {
1845 struct ipvs_master_sync_state *ms;
1848 ipvs->ms = kzalloc(count * sizeof(ipvs->ms[0]), GFP_KERNEL);
1852 for (id = 0; id < count; id++, ms++) {
1853 INIT_LIST_HEAD(&ms->sync_queue);
1854 ms->sync_queue_len = 0;
1855 ms->sync_queue_delay = 0;
1856 INIT_DELAYED_WORK(&ms->master_wakeup_work,
1857 master_wakeup_work_handler);
1861 array = kzalloc(count * sizeof(struct task_struct *),
1868 for (id = 0; id < count; id++) {
1870 tinfo = kmalloc(sizeof(*tinfo), GFP_KERNEL);
1875 if (state == IP_VS_STATE_BACKUP) {
1876 tinfo->buf = kmalloc(ipvs->bcfg.sync_maxlen,
1884 if (state == IP_VS_STATE_MASTER)
1885 result = make_send_sock(ipvs, id, dev, &tinfo->sock);
1887 result = make_receive_sock(ipvs, id, dev, &tinfo->sock);
1891 task = kthread_run(threadfn, tinfo, name, ipvs->gen, id);
1893 result = PTR_ERR(task);
1897 if (state == IP_VS_STATE_MASTER)
1898 ipvs->ms[id].master_thread = task;
1903 /* mark as active */
1905 if (state == IP_VS_STATE_BACKUP)
1906 ipvs->backup_threads = array;
1907 spin_lock_bh(&ipvs->sync_buff_lock);
1908 ipvs->sync_state |= state;
1909 spin_unlock_bh(&ipvs->sync_buff_lock);
1911 mutex_unlock(&ipvs->sync_mutex);
1914 /* increase the module use count */
1915 ip_vs_use_count_inc();
1920 /* We do not need RTNL lock anymore, release it here so that
1921 * sock_release below and in the kthreads can use rtnl_lock
1922 * to leave the mcast group.
1926 while (count-- > 0) {
1927 if (state == IP_VS_STATE_MASTER)
1928 kthread_stop(ipvs->ms[count].master_thread);
1930 kthread_stop(array[count]);
1932 if (!(ipvs->sync_state & IP_VS_STATE_MASTER)) {
1936 mutex_unlock(&ipvs->sync_mutex);
1939 sock_release(tinfo->sock);
1947 mutex_unlock(&ipvs->sync_mutex);
1953 int stop_sync_thread(struct netns_ipvs *ipvs, int state)
1955 struct task_struct **array;
1959 IP_VS_DBG(7, "%s(): pid %d\n", __func__, task_pid_nr(current));
1961 if (state == IP_VS_STATE_MASTER) {
1966 * The lock synchronizes with sb_queue_tail(), so that we don't
1967 * add sync buffers to the queue, when we are already in
1968 * progress of stopping the master sync daemon.
1971 spin_lock_bh(&ipvs->sync_buff_lock);
1972 spin_lock(&ipvs->sync_lock);
1973 ipvs->sync_state &= ~IP_VS_STATE_MASTER;
1974 spin_unlock(&ipvs->sync_lock);
1975 spin_unlock_bh(&ipvs->sync_buff_lock);
1978 for (id = ipvs->threads_mask; id >= 0; id--) {
1979 struct ipvs_master_sync_state *ms = &ipvs->ms[id];
1982 pr_info("stopping master sync thread %d ...\n",
1983 task_pid_nr(ms->master_thread));
1984 cancel_delayed_work_sync(&ms->master_wakeup_work);
1985 ret = kthread_stop(ms->master_thread);
1991 } else if (state == IP_VS_STATE_BACKUP) {
1992 if (!ipvs->backup_threads)
1995 ipvs->sync_state &= ~IP_VS_STATE_BACKUP;
1996 array = ipvs->backup_threads;
1998 for (id = ipvs->threads_mask; id >= 0; id--) {
2001 pr_info("stopping backup sync thread %d ...\n",
2002 task_pid_nr(array[id]));
2003 ret = kthread_stop(array[id]);
2008 ipvs->backup_threads = NULL;
2011 /* decrease the module use count */
2012 ip_vs_use_count_dec();
2018 * Initialize data struct for each netns
2020 int __net_init ip_vs_sync_net_init(struct netns_ipvs *ipvs)
2022 __mutex_init(&ipvs->sync_mutex, "ipvs->sync_mutex", &__ipvs_sync_key);
2023 spin_lock_init(&ipvs->sync_lock);
2024 spin_lock_init(&ipvs->sync_buff_lock);
2028 void ip_vs_sync_net_cleanup(struct netns_ipvs *ipvs)
2032 mutex_lock(&ipvs->sync_mutex);
2033 retc = stop_sync_thread(ipvs, IP_VS_STATE_MASTER);
2034 if (retc && retc != -ESRCH)
2035 pr_err("Failed to stop Master Daemon\n");
2037 retc = stop_sync_thread(ipvs, IP_VS_STATE_BACKUP);
2038 if (retc && retc != -ESRCH)
2039 pr_err("Failed to stop Backup Daemon\n");
2040 mutex_unlock(&ipvs->sync_mutex);