Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * IPVS An implementation of the IP virtual server support for the |
| 3 | * LINUX operating system. IPVS is now implemented as a module |
| 4 | * over the NetFilter framework. IPVS can be used to build a |
| 5 | * high-performance and highly available server based on a |
| 6 | * cluster of servers. |
| 7 | * |
Hans Schillstrom | b880c1f | 2010-11-19 14:25:14 +0100 | [diff] [blame] | 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 |
| 15 | * |
| 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. |
| 19 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | * Authors: Wensong Zhang <wensong@linuxvirtualserver.org> |
| 21 | * |
| 22 | * ip_vs_sync: sync connection info from master load balancer to backups |
| 23 | * through multicast |
| 24 | * |
| 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. |
Hans Schillstrom | b880c1f | 2010-11-19 14:25:14 +0100 | [diff] [blame] | 30 | * Hans Schillstrom : Added Version 1: i.e. IPv6, |
| 31 | * Persistence support, fwmark and time-out. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | */ |
| 33 | |
Hannes Eder | 9aada7a | 2009-07-30 14:29:44 -0700 | [diff] [blame] | 34 | #define KMSG_COMPONENT "IPVS" |
| 35 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/module.h> |
| 38 | #include <linux/slab.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 39 | #include <linux/inetdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 46 | #include <linux/udp.h> |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 47 | #include <linux/err.h> |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 48 | #include <linux/kthread.h> |
Sven Wegener | ba6fd85 | 2008-07-16 11:13:56 +0000 | [diff] [blame] | 49 | #include <linux/wait.h> |
Sven Wegener | e6f225e | 2008-09-19 20:41:56 +0200 | [diff] [blame] | 50 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 52 | #include <asm/unaligned.h> /* Used for ntoh_seq and hton_seq */ |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #include <net/ip.h> |
| 55 | #include <net/sock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | #include <net/ip_vs.h> |
| 58 | |
| 59 | #define IP_VS_SYNC_GROUP 0xe0000051 /* multicast addr - 224.0.0.81 */ |
| 60 | #define IP_VS_SYNC_PORT 8848 /* multicast port */ |
| 61 | |
Hans Schillstrom | 2981bc9 | 2010-11-19 14:25:11 +0100 | [diff] [blame] | 62 | #define SYNC_PROTO_VER 1 /* Protocol version in header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Hans Schillstrom | ae1d48b | 2011-10-11 10:54:35 +0900 | [diff] [blame] | 64 | static struct lock_class_key __ipvs_sync_key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | /* |
| 66 | * IPVS sync connection entry |
Hans Schillstrom | 2981bc9 | 2010-11-19 14:25:11 +0100 | [diff] [blame] | 67 | * Version 0, i.e. original version. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | */ |
Hans Schillstrom | 2981bc9 | 2010-11-19 14:25:11 +0100 | [diff] [blame] | 69 | struct ip_vs_sync_conn_v0 { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | __u8 reserved; |
| 71 | |
| 72 | /* Protocol, addresses and port numbers */ |
| 73 | __u8 protocol; /* Which protocol (TCP/UDP) */ |
Al Viro | 014d730 | 2006-09-28 14:29:52 -0700 | [diff] [blame] | 74 | __be16 cport; |
| 75 | __be16 vport; |
| 76 | __be16 dport; |
| 77 | __be32 caddr; /* client address */ |
| 78 | __be32 vaddr; /* virtual address */ |
| 79 | __be32 daddr; /* destination address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | /* Flags and state transition */ |
Al Viro | 014d730 | 2006-09-28 14:29:52 -0700 | [diff] [blame] | 82 | __be16 flags; /* status flags */ |
| 83 | __be16 state; /* state info */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | /* The sequence options start here */ |
| 86 | }; |
| 87 | |
| 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 */ |
| 91 | }; |
| 92 | |
Hans Schillstrom | 2981bc9 | 2010-11-19 14:25:11 +0100 | [diff] [blame] | 93 | /* |
| 94 | Sync Connection format (sync_conn) |
| 95 | |
| 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 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 126 | */ |
| 127 | |
| 128 | /* |
| 129 | * Type 0, IPv4 sync connection format |
| 130 | */ |
| 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 */ |
| 149 | }; |
| 150 | /* |
| 151 | * Type 2 messages IPv6 |
| 152 | */ |
| 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 */ |
| 171 | }; |
| 172 | |
| 173 | union ip_vs_sync_conn { |
| 174 | struct ip_vs_sync_v4 v4; |
| 175 | struct ip_vs_sync_v6 v6; |
| 176 | }; |
| 177 | |
| 178 | /* Bits in Type field in above */ |
| 179 | #define STYPE_INET6 0 |
| 180 | #define STYPE_F_INET6 (1 << STYPE_INET6) |
| 181 | |
| 182 | #define SVER_SHIFT 12 /* Shift to get version */ |
| 183 | #define SVER_MASK 0x0fff /* Mask to strip version */ |
| 184 | |
| 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 |
| 189 | |
| 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)) |
| 194 | |
Neil Horman | cc0191a | 2007-06-18 22:33:20 -0700 | [diff] [blame] | 195 | struct ip_vs_sync_thread_data { |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 196 | struct net *net; |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 197 | struct socket *sock; |
| 198 | char *buf; |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 199 | int id; |
Neil Horman | cc0191a | 2007-06-18 22:33:20 -0700 | [diff] [blame] | 200 | }; |
| 201 | |
Hans Schillstrom | 2981bc9 | 2010-11-19 14:25:11 +0100 | [diff] [blame] | 202 | /* Version 0 definition of packet sizes */ |
| 203 | #define SIMPLE_CONN_SIZE (sizeof(struct ip_vs_sync_conn_v0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | #define FULL_CONN_SIZE \ |
Hans Schillstrom | 2981bc9 | 2010-11-19 14:25:11 +0100 | [diff] [blame] | 205 | (sizeof(struct ip_vs_sync_conn_v0) + sizeof(struct ip_vs_sync_conn_options)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
| 207 | |
| 208 | /* |
Hans Schillstrom | 2981bc9 | 2010-11-19 14:25:11 +0100 | [diff] [blame] | 209 | The master mulitcasts messages (Datagrams) to the backup load balancers |
| 210 | in the following format. |
| 211 | |
| 212 | Version 1: |
| 213 | Note, first byte should be Zero, so ver 0 receivers will drop the packet. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 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 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Hans Schillstrom | 2981bc9 | 2010-11-19 14:25:11 +0100 | [diff] [blame] | 218 | | 0 | SyncID | Size | |
| 219 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 220 | | Count Conns | Version | Reserved, set to Zero | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 222 | | | |
| 223 | | IPVS Sync Connection (1) | |
| 224 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 225 | | . | |
Hans Schillstrom | 2981bc9 | 2010-11-19 14:25:11 +0100 | [diff] [blame] | 226 | ~ . ~ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | | . | |
| 228 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 229 | | | |
| 230 | | IPVS Sync Connection (n) | |
| 231 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Hans Schillstrom | 2981bc9 | 2010-11-19 14:25:11 +0100 | [diff] [blame] | 232 | |
| 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) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | */ |
| 241 | |
| 242 | #define SYNC_MESG_HEADER_LEN 4 |
Sven Wegener | e6f225e | 2008-09-19 20:41:56 +0200 | [diff] [blame] | 243 | #define MAX_CONNS_PER_SYNCBUFF 255 /* nr_conns in ip_vs_sync_mesg is 8 bit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Hans Schillstrom | 2981bc9 | 2010-11-19 14:25:11 +0100 | [diff] [blame] | 245 | /* Version 0 header */ |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 246 | struct ip_vs_sync_mesg_v0 { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | __u8 nr_conns; |
| 248 | __u8 syncid; |
Simon Horman | 3856143 | 2013-04-19 10:43:44 +0900 | [diff] [blame] | 249 | __be16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
| 251 | /* ip_vs_sync_conn entries start here */ |
| 252 | }; |
| 253 | |
Hans Schillstrom | 2981bc9 | 2010-11-19 14:25:11 +0100 | [diff] [blame] | 254 | /* Version 1 header */ |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 255 | struct ip_vs_sync_mesg { |
Hans Schillstrom | 2981bc9 | 2010-11-19 14:25:11 +0100 | [diff] [blame] | 256 | __u8 reserved; /* must be zero */ |
| 257 | __u8 syncid; |
Simon Horman | 3856143 | 2013-04-19 10:43:44 +0900 | [diff] [blame] | 258 | __be16 size; |
Hans Schillstrom | 2981bc9 | 2010-11-19 14:25:11 +0100 | [diff] [blame] | 259 | __u8 nr_conns; |
| 260 | __s8 version; /* SYNC_PROTO_VER */ |
| 261 | __u16 spare; |
| 262 | /* ip_vs_sync_conn entries start here */ |
| 263 | }; |
| 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | struct ip_vs_sync_buff { |
| 266 | struct list_head list; |
| 267 | unsigned long firstuse; |
| 268 | |
| 269 | /* pointers for the message data */ |
| 270 | struct ip_vs_sync_mesg *mesg; |
| 271 | unsigned char *head; |
| 272 | unsigned char *end; |
| 273 | }; |
| 274 | |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 275 | /* |
| 276 | * Copy of struct ip_vs_seq |
| 277 | * From unaligned network order to aligned host order |
| 278 | */ |
| 279 | static void ntoh_seq(struct ip_vs_seq *no, struct ip_vs_seq *ho) |
| 280 | { |
| 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); |
| 284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 286 | /* |
| 287 | * Copy of struct ip_vs_seq |
| 288 | * From Aligned host order to unaligned network order |
| 289 | */ |
| 290 | static void hton_seq(struct ip_vs_seq *ho, struct ip_vs_seq *no) |
| 291 | { |
| 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); |
| 295 | } |
| 296 | |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 297 | static inline struct ip_vs_sync_buff * |
| 298 | sb_dequeue(struct netns_ipvs *ipvs, struct ipvs_master_sync_state *ms) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { |
| 300 | struct ip_vs_sync_buff *sb; |
| 301 | |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 302 | spin_lock_bh(&ipvs->sync_lock); |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 303 | if (list_empty(&ms->sync_queue)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | sb = NULL; |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 305 | __set_current_state(TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } else { |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 307 | sb = list_entry(ms->sync_queue.next, struct ip_vs_sync_buff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | list); |
| 309 | list_del(&sb->list); |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 310 | ms->sync_queue_len--; |
| 311 | if (!ms->sync_queue_len) |
| 312 | ms->sync_queue_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | } |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 314 | spin_unlock_bh(&ipvs->sync_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
| 316 | return sb; |
| 317 | } |
| 318 | |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 319 | /* |
| 320 | * Create a new sync buffer for Version 1 proto. |
| 321 | */ |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 322 | static inline struct ip_vs_sync_buff * |
| 323 | ip_vs_sync_buff_create(struct netns_ipvs *ipvs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
| 325 | struct ip_vs_sync_buff *sb; |
| 326 | |
| 327 | if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC))) |
| 328 | return NULL; |
| 329 | |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 330 | sb->mesg = kmalloc(ipvs->send_mesg_maxlen, GFP_ATOMIC); |
| 331 | if (!sb->mesg) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | kfree(sb); |
| 333 | return NULL; |
| 334 | } |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 335 | sb->mesg->reserved = 0; /* old nr_conns i.e. must be zero now */ |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 336 | sb->mesg->version = SYNC_PROTO_VER; |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 337 | sb->mesg->syncid = ipvs->master_syncid; |
Simon Horman | 3856143 | 2013-04-19 10:43:44 +0900 | [diff] [blame] | 338 | sb->mesg->size = htons(sizeof(struct ip_vs_sync_mesg)); |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 339 | sb->mesg->nr_conns = 0; |
| 340 | sb->mesg->spare = 0; |
| 341 | sb->head = (unsigned char *)sb->mesg + sizeof(struct ip_vs_sync_mesg); |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 342 | sb->end = (unsigned char *)sb->mesg + ipvs->send_mesg_maxlen; |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | sb->firstuse = jiffies; |
| 345 | return sb; |
| 346 | } |
| 347 | |
| 348 | static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff *sb) |
| 349 | { |
| 350 | kfree(sb->mesg); |
| 351 | kfree(sb); |
| 352 | } |
| 353 | |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 354 | static inline void sb_queue_tail(struct netns_ipvs *ipvs, |
| 355 | struct ipvs_master_sync_state *ms) |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 356 | { |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 357 | struct ip_vs_sync_buff *sb = ms->sync_buff; |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 358 | |
| 359 | spin_lock(&ipvs->sync_lock); |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 360 | if (ipvs->sync_state & IP_VS_STATE_MASTER && |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 361 | ms->sync_queue_len < sysctl_sync_qlen_max(ipvs)) { |
| 362 | if (!ms->sync_queue_len) |
| 363 | schedule_delayed_work(&ms->master_wakeup_work, |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 364 | max(IPVS_SYNC_SEND_DELAY, 1)); |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 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); |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 369 | } else |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 370 | ip_vs_sync_buff_release(sb); |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 371 | spin_unlock(&ipvs->sync_lock); |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | /* |
| 375 | * Get the current sync buffer if it has been created for more |
| 376 | * than the specified time or the specified time is zero. |
| 377 | */ |
| 378 | static inline struct ip_vs_sync_buff * |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 379 | get_curr_sync_buff(struct netns_ipvs *ipvs, struct ipvs_master_sync_state *ms, |
| 380 | unsigned long time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | { |
| 382 | struct ip_vs_sync_buff *sb; |
| 383 | |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 384 | spin_lock_bh(&ipvs->sync_buff_lock); |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 385 | sb = ms->sync_buff; |
| 386 | if (sb && time_after_eq(jiffies - sb->firstuse, time)) { |
| 387 | ms->sync_buff = NULL; |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 388 | __set_current_state(TASK_RUNNING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | } else |
| 390 | sb = NULL; |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 391 | spin_unlock_bh(&ipvs->sync_buff_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | return sb; |
| 393 | } |
| 394 | |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 395 | static inline int |
| 396 | select_master_thread_id(struct netns_ipvs *ipvs, struct ip_vs_conn *cp) |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 397 | { |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 398 | return ((long) cp >> (1 + ilog2(sizeof(*cp)))) & ipvs->threads_mask; |
Hans Schillstrom | b880c1f | 2010-11-19 14:25:14 +0100 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /* |
| 402 | * Create a new sync buffer for Version 0 proto. |
| 403 | */ |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 404 | static inline struct ip_vs_sync_buff * |
| 405 | ip_vs_sync_buff_create_v0(struct netns_ipvs *ipvs) |
Hans Schillstrom | b880c1f | 2010-11-19 14:25:14 +0100 | [diff] [blame] | 406 | { |
| 407 | struct ip_vs_sync_buff *sb; |
| 408 | struct ip_vs_sync_mesg_v0 *mesg; |
| 409 | |
| 410 | if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC))) |
| 411 | return NULL; |
| 412 | |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 413 | sb->mesg = kmalloc(ipvs->send_mesg_maxlen, GFP_ATOMIC); |
| 414 | if (!sb->mesg) { |
Hans Schillstrom | b880c1f | 2010-11-19 14:25:14 +0100 | [diff] [blame] | 415 | kfree(sb); |
| 416 | return NULL; |
| 417 | } |
| 418 | mesg = (struct ip_vs_sync_mesg_v0 *)sb->mesg; |
| 419 | mesg->nr_conns = 0; |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 420 | mesg->syncid = ipvs->master_syncid; |
Simon Horman | 3856143 | 2013-04-19 10:43:44 +0900 | [diff] [blame] | 421 | mesg->size = htons(sizeof(struct ip_vs_sync_mesg_v0)); |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 422 | sb->head = (unsigned char *)mesg + sizeof(struct ip_vs_sync_mesg_v0); |
| 423 | sb->end = (unsigned char *)mesg + ipvs->send_mesg_maxlen; |
Hans Schillstrom | b880c1f | 2010-11-19 14:25:14 +0100 | [diff] [blame] | 424 | sb->firstuse = jiffies; |
| 425 | return sb; |
| 426 | } |
| 427 | |
Julian Anastasov | 4d0c875 | 2013-06-24 22:44:41 +0300 | [diff] [blame] | 428 | /* Check if connection is controlled by persistence */ |
| 429 | static inline bool in_persistence(struct ip_vs_conn *cp) |
| 430 | { |
| 431 | for (cp = cp->control; cp; cp = cp->control) { |
| 432 | if (cp->flags & IP_VS_CONN_F_TEMPLATE) |
| 433 | return true; |
| 434 | } |
| 435 | return false; |
| 436 | } |
| 437 | |
Julian Anastasov | 749c42b | 2012-04-24 23:46:40 +0300 | [diff] [blame] | 438 | /* Check if conn should be synced. |
| 439 | * pkts: conn packets, use sysctl_sync_threshold to avoid packet check |
| 440 | * - (1) sync_refresh_period: reduce sync rate. Additionally, retry |
| 441 | * sync_retries times with period of sync_refresh_period/8 |
| 442 | * - (2) if both sync_refresh_period and sync_period are 0 send sync only |
| 443 | * for state changes or only once when pkts matches sync_threshold |
| 444 | * - (3) templates: rate can be reduced only with sync_refresh_period or |
| 445 | * with (2) |
| 446 | */ |
| 447 | static int ip_vs_sync_conn_needed(struct netns_ipvs *ipvs, |
| 448 | struct ip_vs_conn *cp, int pkts) |
| 449 | { |
| 450 | unsigned long orig = ACCESS_ONCE(cp->sync_endtime); |
| 451 | unsigned long now = jiffies; |
| 452 | unsigned long n = (now + cp->timeout) & ~3UL; |
| 453 | unsigned int sync_refresh_period; |
| 454 | int sync_period; |
| 455 | int force; |
| 456 | |
| 457 | /* Check if we sync in current state */ |
| 458 | if (unlikely(cp->flags & IP_VS_CONN_F_TEMPLATE)) |
| 459 | force = 0; |
Julian Anastasov | 4d0c875 | 2013-06-24 22:44:41 +0300 | [diff] [blame] | 460 | else if (unlikely(sysctl_sync_persist_mode(ipvs) && in_persistence(cp))) |
| 461 | return 0; |
Julian Anastasov | 749c42b | 2012-04-24 23:46:40 +0300 | [diff] [blame] | 462 | else if (likely(cp->protocol == IPPROTO_TCP)) { |
| 463 | if (!((1 << cp->state) & |
| 464 | ((1 << IP_VS_TCP_S_ESTABLISHED) | |
| 465 | (1 << IP_VS_TCP_S_FIN_WAIT) | |
| 466 | (1 << IP_VS_TCP_S_CLOSE) | |
| 467 | (1 << IP_VS_TCP_S_CLOSE_WAIT) | |
| 468 | (1 << IP_VS_TCP_S_TIME_WAIT)))) |
| 469 | return 0; |
| 470 | force = cp->state != cp->old_state; |
| 471 | if (force && cp->state != IP_VS_TCP_S_ESTABLISHED) |
| 472 | goto set; |
| 473 | } else if (unlikely(cp->protocol == IPPROTO_SCTP)) { |
| 474 | if (!((1 << cp->state) & |
| 475 | ((1 << IP_VS_SCTP_S_ESTABLISHED) | |
Julian Anastasov | 61e7c42 | 2013-06-18 10:08:07 +0300 | [diff] [blame] | 476 | (1 << IP_VS_SCTP_S_SHUTDOWN_SENT) | |
| 477 | (1 << IP_VS_SCTP_S_SHUTDOWN_RECEIVED) | |
| 478 | (1 << IP_VS_SCTP_S_SHUTDOWN_ACK_SENT) | |
| 479 | (1 << IP_VS_SCTP_S_CLOSED)))) |
Julian Anastasov | 749c42b | 2012-04-24 23:46:40 +0300 | [diff] [blame] | 480 | return 0; |
| 481 | force = cp->state != cp->old_state; |
| 482 | if (force && cp->state != IP_VS_SCTP_S_ESTABLISHED) |
| 483 | goto set; |
| 484 | } else { |
| 485 | /* UDP or another protocol with single state */ |
| 486 | force = 0; |
| 487 | } |
| 488 | |
| 489 | sync_refresh_period = sysctl_sync_refresh_period(ipvs); |
| 490 | if (sync_refresh_period > 0) { |
| 491 | long diff = n - orig; |
| 492 | long min_diff = max(cp->timeout >> 1, 10UL * HZ); |
| 493 | |
| 494 | /* Avoid sync if difference is below sync_refresh_period |
| 495 | * and below the half timeout. |
| 496 | */ |
| 497 | if (abs(diff) < min_t(long, sync_refresh_period, min_diff)) { |
| 498 | int retries = orig & 3; |
| 499 | |
| 500 | if (retries >= sysctl_sync_retries(ipvs)) |
| 501 | return 0; |
| 502 | if (time_before(now, orig - cp->timeout + |
| 503 | (sync_refresh_period >> 3))) |
| 504 | return 0; |
| 505 | n |= retries + 1; |
| 506 | } |
| 507 | } |
| 508 | sync_period = sysctl_sync_period(ipvs); |
| 509 | if (sync_period > 0) { |
| 510 | if (!(cp->flags & IP_VS_CONN_F_TEMPLATE) && |
| 511 | pkts % sync_period != sysctl_sync_threshold(ipvs)) |
| 512 | return 0; |
| 513 | } else if (sync_refresh_period <= 0 && |
| 514 | pkts != sysctl_sync_threshold(ipvs)) |
| 515 | return 0; |
| 516 | |
| 517 | set: |
| 518 | cp->old_state = cp->state; |
| 519 | n = cmpxchg(&cp->sync_endtime, orig, n); |
| 520 | return n == orig || force; |
| 521 | } |
| 522 | |
Hans Schillstrom | b880c1f | 2010-11-19 14:25:14 +0100 | [diff] [blame] | 523 | /* |
| 524 | * Version 0 , could be switched in by sys_ctl. |
| 525 | * Add an ip_vs_conn information into the current sync_buff. |
| 526 | */ |
Julian Anastasov | 749c42b | 2012-04-24 23:46:40 +0300 | [diff] [blame] | 527 | static void ip_vs_sync_conn_v0(struct net *net, struct ip_vs_conn *cp, |
| 528 | int pkts) |
Hans Schillstrom | b880c1f | 2010-11-19 14:25:14 +0100 | [diff] [blame] | 529 | { |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 530 | struct netns_ipvs *ipvs = net_ipvs(net); |
Hans Schillstrom | b880c1f | 2010-11-19 14:25:14 +0100 | [diff] [blame] | 531 | struct ip_vs_sync_mesg_v0 *m; |
| 532 | struct ip_vs_sync_conn_v0 *s; |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 533 | struct ip_vs_sync_buff *buff; |
| 534 | struct ipvs_master_sync_state *ms; |
| 535 | int id; |
Hans Schillstrom | b880c1f | 2010-11-19 14:25:14 +0100 | [diff] [blame] | 536 | int len; |
| 537 | |
| 538 | if (unlikely(cp->af != AF_INET)) |
| 539 | return; |
| 540 | /* Do not sync ONE PACKET */ |
| 541 | if (cp->flags & IP_VS_CONN_F_ONE_PACKET) |
| 542 | return; |
| 543 | |
Julian Anastasov | 749c42b | 2012-04-24 23:46:40 +0300 | [diff] [blame] | 544 | if (!ip_vs_sync_conn_needed(ipvs, cp, pkts)) |
| 545 | return; |
| 546 | |
Julian Anastasov | ac69269 | 2013-03-22 11:46:54 +0200 | [diff] [blame] | 547 | spin_lock_bh(&ipvs->sync_buff_lock); |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 548 | if (!(ipvs->sync_state & IP_VS_STATE_MASTER)) { |
Julian Anastasov | ac69269 | 2013-03-22 11:46:54 +0200 | [diff] [blame] | 549 | spin_unlock_bh(&ipvs->sync_buff_lock); |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 550 | return; |
| 551 | } |
| 552 | |
| 553 | id = select_master_thread_id(ipvs, cp); |
| 554 | ms = &ipvs->ms[id]; |
| 555 | buff = ms->sync_buff; |
| 556 | if (buff) { |
| 557 | m = (struct ip_vs_sync_mesg_v0 *) buff->mesg; |
| 558 | /* Send buffer if it is for v1 */ |
| 559 | if (!m->nr_conns) { |
| 560 | sb_queue_tail(ipvs, ms); |
| 561 | ms->sync_buff = NULL; |
| 562 | buff = NULL; |
| 563 | } |
| 564 | } |
| 565 | if (!buff) { |
| 566 | buff = ip_vs_sync_buff_create_v0(ipvs); |
| 567 | if (!buff) { |
Julian Anastasov | ac69269 | 2013-03-22 11:46:54 +0200 | [diff] [blame] | 568 | spin_unlock_bh(&ipvs->sync_buff_lock); |
Hans Schillstrom | b880c1f | 2010-11-19 14:25:14 +0100 | [diff] [blame] | 569 | pr_err("ip_vs_sync_buff_create failed.\n"); |
| 570 | return; |
| 571 | } |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 572 | ms->sync_buff = buff; |
Hans Schillstrom | b880c1f | 2010-11-19 14:25:14 +0100 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | len = (cp->flags & IP_VS_CONN_F_SEQ_MASK) ? FULL_CONN_SIZE : |
| 576 | SIMPLE_CONN_SIZE; |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 577 | m = (struct ip_vs_sync_mesg_v0 *) buff->mesg; |
| 578 | s = (struct ip_vs_sync_conn_v0 *) buff->head; |
Hans Schillstrom | b880c1f | 2010-11-19 14:25:14 +0100 | [diff] [blame] | 579 | |
| 580 | /* copy members */ |
| 581 | s->reserved = 0; |
| 582 | s->protocol = cp->protocol; |
| 583 | s->cport = cp->cport; |
| 584 | s->vport = cp->vport; |
| 585 | s->dport = cp->dport; |
| 586 | s->caddr = cp->caddr.ip; |
| 587 | s->vaddr = cp->vaddr.ip; |
| 588 | s->daddr = cp->daddr.ip; |
| 589 | s->flags = htons(cp->flags & ~IP_VS_CONN_F_HASHED); |
| 590 | s->state = htons(cp->state); |
| 591 | if (cp->flags & IP_VS_CONN_F_SEQ_MASK) { |
| 592 | struct ip_vs_sync_conn_options *opt = |
| 593 | (struct ip_vs_sync_conn_options *)&s[1]; |
| 594 | memcpy(opt, &cp->in_seq, sizeof(*opt)); |
| 595 | } |
| 596 | |
| 597 | m->nr_conns++; |
Simon Horman | 3856143 | 2013-04-19 10:43:44 +0900 | [diff] [blame] | 598 | m->size = htons(ntohs(m->size) + len); |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 599 | buff->head += len; |
Hans Schillstrom | b880c1f | 2010-11-19 14:25:14 +0100 | [diff] [blame] | 600 | |
| 601 | /* check if there is a space for next one */ |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 602 | if (buff->head + FULL_CONN_SIZE > buff->end) { |
| 603 | sb_queue_tail(ipvs, ms); |
| 604 | ms->sync_buff = NULL; |
Hans Schillstrom | b880c1f | 2010-11-19 14:25:14 +0100 | [diff] [blame] | 605 | } |
Julian Anastasov | ac69269 | 2013-03-22 11:46:54 +0200 | [diff] [blame] | 606 | spin_unlock_bh(&ipvs->sync_buff_lock); |
Hans Schillstrom | b880c1f | 2010-11-19 14:25:14 +0100 | [diff] [blame] | 607 | |
| 608 | /* synchronize its controller if it has */ |
Julian Anastasov | 749c42b | 2012-04-24 23:46:40 +0300 | [diff] [blame] | 609 | cp = cp->control; |
| 610 | if (cp) { |
| 611 | if (cp->flags & IP_VS_CONN_F_TEMPLATE) |
| 612 | pkts = atomic_add_return(1, &cp->in_pkts); |
| 613 | else |
| 614 | pkts = sysctl_sync_threshold(ipvs); |
| 615 | ip_vs_sync_conn(net, cp->control, pkts); |
| 616 | } |
Hans Schillstrom | b880c1f | 2010-11-19 14:25:14 +0100 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | * Add an ip_vs_conn information into the current sync_buff. |
| 621 | * Called by ip_vs_in. |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 622 | * Sending Version 1 messages |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | */ |
Julian Anastasov | 749c42b | 2012-04-24 23:46:40 +0300 | [diff] [blame] | 624 | void ip_vs_sync_conn(struct net *net, struct ip_vs_conn *cp, int pkts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | { |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 626 | struct netns_ipvs *ipvs = net_ipvs(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | struct ip_vs_sync_mesg *m; |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 628 | union ip_vs_sync_conn *s; |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 629 | struct ip_vs_sync_buff *buff; |
| 630 | struct ipvs_master_sync_state *ms; |
| 631 | int id; |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 632 | __u8 *p; |
| 633 | unsigned int len, pe_name_len, pad; |
| 634 | |
Hans Schillstrom | b880c1f | 2010-11-19 14:25:14 +0100 | [diff] [blame] | 635 | /* Handle old version of the protocol */ |
Simon Horman | 7532e8d | 2011-02-04 18:33:01 +0900 | [diff] [blame] | 636 | if (sysctl_sync_ver(ipvs) == 0) { |
Julian Anastasov | 749c42b | 2012-04-24 23:46:40 +0300 | [diff] [blame] | 637 | ip_vs_sync_conn_v0(net, cp, pkts); |
Hans Schillstrom | b880c1f | 2010-11-19 14:25:14 +0100 | [diff] [blame] | 638 | return; |
| 639 | } |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 640 | /* Do not sync ONE PACKET */ |
| 641 | if (cp->flags & IP_VS_CONN_F_ONE_PACKET) |
| 642 | goto control; |
| 643 | sloop: |
Julian Anastasov | 749c42b | 2012-04-24 23:46:40 +0300 | [diff] [blame] | 644 | if (!ip_vs_sync_conn_needed(ipvs, cp, pkts)) |
| 645 | goto control; |
| 646 | |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 647 | /* Sanity checks */ |
| 648 | pe_name_len = 0; |
| 649 | if (cp->pe_data_len) { |
| 650 | if (!cp->pe_data || !cp->dest) { |
| 651 | IP_VS_ERR_RL("SYNC, connection pe_data invalid\n"); |
| 652 | return; |
| 653 | } |
| 654 | pe_name_len = strnlen(cp->pe->name, IP_VS_PENAME_MAXLEN); |
| 655 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | |
Julian Anastasov | ac69269 | 2013-03-22 11:46:54 +0200 | [diff] [blame] | 657 | spin_lock_bh(&ipvs->sync_buff_lock); |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 658 | if (!(ipvs->sync_state & IP_VS_STATE_MASTER)) { |
Julian Anastasov | ac69269 | 2013-03-22 11:46:54 +0200 | [diff] [blame] | 659 | spin_unlock_bh(&ipvs->sync_buff_lock); |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 660 | return; |
| 661 | } |
| 662 | |
| 663 | id = select_master_thread_id(ipvs, cp); |
| 664 | ms = &ipvs->ms[id]; |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 665 | |
| 666 | #ifdef CONFIG_IP_VS_IPV6 |
| 667 | if (cp->af == AF_INET6) |
| 668 | len = sizeof(struct ip_vs_sync_v6); |
| 669 | else |
| 670 | #endif |
| 671 | len = sizeof(struct ip_vs_sync_v4); |
| 672 | |
| 673 | if (cp->flags & IP_VS_CONN_F_SEQ_MASK) |
| 674 | len += sizeof(struct ip_vs_sync_conn_options) + 2; |
| 675 | |
| 676 | if (cp->pe_data_len) |
| 677 | len += cp->pe_data_len + 2; /* + Param hdr field */ |
| 678 | if (pe_name_len) |
| 679 | len += pe_name_len + 2; |
| 680 | |
| 681 | /* check if there is a space for this one */ |
| 682 | pad = 0; |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 683 | buff = ms->sync_buff; |
| 684 | if (buff) { |
| 685 | m = buff->mesg; |
| 686 | pad = (4 - (size_t) buff->head) & 3; |
| 687 | /* Send buffer if it is for v0 */ |
| 688 | if (buff->head + len + pad > buff->end || m->reserved) { |
| 689 | sb_queue_tail(ipvs, ms); |
| 690 | ms->sync_buff = NULL; |
| 691 | buff = NULL; |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 692 | pad = 0; |
| 693 | } |
| 694 | } |
| 695 | |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 696 | if (!buff) { |
| 697 | buff = ip_vs_sync_buff_create(ipvs); |
| 698 | if (!buff) { |
Julian Anastasov | ac69269 | 2013-03-22 11:46:54 +0200 | [diff] [blame] | 699 | spin_unlock_bh(&ipvs->sync_buff_lock); |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 700 | pr_err("ip_vs_sync_buff_create failed.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | return; |
| 702 | } |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 703 | ms->sync_buff = buff; |
| 704 | m = buff->mesg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 707 | p = buff->head; |
| 708 | buff->head += pad + len; |
Simon Horman | 3856143 | 2013-04-19 10:43:44 +0900 | [diff] [blame] | 709 | m->size = htons(ntohs(m->size) + pad + len); |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 710 | /* Add ev. padding from prev. sync_conn */ |
| 711 | while (pad--) |
| 712 | *(p++) = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 714 | s = (union ip_vs_sync_conn *)p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 716 | /* Set message type & copy members */ |
| 717 | s->v4.type = (cp->af == AF_INET6 ? STYPE_F_INET6 : 0); |
| 718 | s->v4.ver_size = htons(len & SVER_MASK); /* Version 0 */ |
| 719 | s->v4.flags = htonl(cp->flags & ~IP_VS_CONN_F_HASHED); |
| 720 | s->v4.state = htons(cp->state); |
| 721 | s->v4.protocol = cp->protocol; |
| 722 | s->v4.cport = cp->cport; |
| 723 | s->v4.vport = cp->vport; |
| 724 | s->v4.dport = cp->dport; |
| 725 | s->v4.fwmark = htonl(cp->fwmark); |
| 726 | s->v4.timeout = htonl(cp->timeout / HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | m->nr_conns++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 729 | #ifdef CONFIG_IP_VS_IPV6 |
| 730 | if (cp->af == AF_INET6) { |
| 731 | p += sizeof(struct ip_vs_sync_v6); |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 732 | s->v6.caddr = cp->caddr.in6; |
| 733 | s->v6.vaddr = cp->vaddr.in6; |
| 734 | s->v6.daddr = cp->daddr.in6; |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 735 | } else |
| 736 | #endif |
| 737 | { |
| 738 | p += sizeof(struct ip_vs_sync_v4); /* options ptr */ |
| 739 | s->v4.caddr = cp->caddr.ip; |
| 740 | s->v4.vaddr = cp->vaddr.ip; |
| 741 | s->v4.daddr = cp->daddr.ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | } |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 743 | if (cp->flags & IP_VS_CONN_F_SEQ_MASK) { |
| 744 | *(p++) = IPVS_OPT_SEQ_DATA; |
| 745 | *(p++) = sizeof(struct ip_vs_sync_conn_options); |
| 746 | hton_seq((struct ip_vs_seq *)p, &cp->in_seq); |
| 747 | p += sizeof(struct ip_vs_seq); |
| 748 | hton_seq((struct ip_vs_seq *)p, &cp->out_seq); |
| 749 | p += sizeof(struct ip_vs_seq); |
| 750 | } |
| 751 | /* Handle pe data */ |
| 752 | if (cp->pe_data_len && cp->pe_data) { |
| 753 | *(p++) = IPVS_OPT_PE_DATA; |
| 754 | *(p++) = cp->pe_data_len; |
| 755 | memcpy(p, cp->pe_data, cp->pe_data_len); |
| 756 | p += cp->pe_data_len; |
| 757 | if (pe_name_len) { |
| 758 | /* Add PE_NAME */ |
| 759 | *(p++) = IPVS_OPT_PE_NAME; |
| 760 | *(p++) = pe_name_len; |
| 761 | memcpy(p, cp->pe->name, pe_name_len); |
| 762 | p += pe_name_len; |
| 763 | } |
| 764 | } |
| 765 | |
Julian Anastasov | ac69269 | 2013-03-22 11:46:54 +0200 | [diff] [blame] | 766 | spin_unlock_bh(&ipvs->sync_buff_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 768 | control: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | /* synchronize its controller if it has */ |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 770 | cp = cp->control; |
| 771 | if (!cp) |
| 772 | return; |
Julian Anastasov | 749c42b | 2012-04-24 23:46:40 +0300 | [diff] [blame] | 773 | if (cp->flags & IP_VS_CONN_F_TEMPLATE) |
| 774 | pkts = atomic_add_return(1, &cp->in_pkts); |
| 775 | else |
| 776 | pkts = sysctl_sync_threshold(ipvs); |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 777 | goto sloop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 780 | /* |
| 781 | * fill_param used by version 1 |
| 782 | */ |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 783 | static inline int |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 784 | ip_vs_conn_fill_param_sync(struct net *net, int af, union ip_vs_sync_conn *sc, |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 785 | struct ip_vs_conn_param *p, |
| 786 | __u8 *pe_data, unsigned int pe_data_len, |
| 787 | __u8 *pe_name, unsigned int pe_name_len) |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 788 | { |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 789 | #ifdef CONFIG_IP_VS_IPV6 |
| 790 | if (af == AF_INET6) |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 791 | ip_vs_conn_fill_param(net, af, sc->v6.protocol, |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 792 | (const union nf_inet_addr *)&sc->v6.caddr, |
| 793 | sc->v6.cport, |
| 794 | (const union nf_inet_addr *)&sc->v6.vaddr, |
| 795 | sc->v6.vport, p); |
| 796 | else |
| 797 | #endif |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 798 | ip_vs_conn_fill_param(net, af, sc->v4.protocol, |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 799 | (const union nf_inet_addr *)&sc->v4.caddr, |
| 800 | sc->v4.cport, |
| 801 | (const union nf_inet_addr *)&sc->v4.vaddr, |
| 802 | sc->v4.vport, p); |
| 803 | /* Handle pe data */ |
| 804 | if (pe_data_len) { |
| 805 | if (pe_name_len) { |
| 806 | char buff[IP_VS_PENAME_MAXLEN+1]; |
| 807 | |
| 808 | memcpy(buff, pe_name, pe_name_len); |
| 809 | buff[pe_name_len]=0; |
| 810 | p->pe = __ip_vs_pe_getbyname(buff); |
| 811 | if (!p->pe) { |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 812 | IP_VS_DBG(3, "BACKUP, no %s engine found/loaded\n", |
| 813 | buff); |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 814 | return 1; |
| 815 | } |
| 816 | } else { |
| 817 | IP_VS_ERR_RL("BACKUP, Invalid PE parameters\n"); |
| 818 | return 1; |
| 819 | } |
| 820 | |
Shan Wei | 6060c74 | 2011-03-07 10:11:34 +0800 | [diff] [blame] | 821 | p->pe_data = kmemdup(pe_data, pe_data_len, GFP_ATOMIC); |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 822 | if (!p->pe_data) { |
Markus Elfring | 982f405 | 2014-11-18 20:37:05 +0100 | [diff] [blame] | 823 | module_put(p->pe->module); |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 824 | return -ENOMEM; |
| 825 | } |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 826 | p->pe_data_len = pe_data_len; |
| 827 | } |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 828 | return 0; |
| 829 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | |
| 831 | /* |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 832 | * Connection Add / Update. |
| 833 | * Common for version 0 and 1 reception of backup sync_conns. |
| 834 | * Param: ... |
| 835 | * timeout is in sec. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | */ |
Hans Schillstrom | 9bbac6a | 2011-01-03 14:44:52 +0100 | [diff] [blame] | 837 | static void ip_vs_proc_conn(struct net *net, struct ip_vs_conn_param *param, |
| 838 | unsigned int flags, unsigned int state, |
| 839 | unsigned int protocol, unsigned int type, |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 840 | const union nf_inet_addr *daddr, __be16 dport, |
| 841 | unsigned long timeout, __u32 fwmark, |
Hans Schillstrom | 9bbac6a | 2011-01-03 14:44:52 +0100 | [diff] [blame] | 842 | struct ip_vs_sync_conn_options *opt) |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 843 | { |
| 844 | struct ip_vs_dest *dest; |
| 845 | struct ip_vs_conn *cp; |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 846 | struct netns_ipvs *ipvs = net_ipvs(net); |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 847 | |
Marcelo Ricardo Leitner | d752c36 | 2015-02-23 15:02:34 -0300 | [diff] [blame] | 848 | if (!(flags & IP_VS_CONN_F_TEMPLATE)) { |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 849 | cp = ip_vs_conn_in_get(param); |
Marcelo Ricardo Leitner | d752c36 | 2015-02-23 15:02:34 -0300 | [diff] [blame] | 850 | if (cp && ((cp->dport != dport) || |
| 851 | !ip_vs_addr_equal(cp->daf, &cp->daddr, daddr))) { |
| 852 | if (!(flags & IP_VS_CONN_F_INACTIVE)) { |
| 853 | ip_vs_conn_expire_now(cp); |
| 854 | __ip_vs_conn_put(cp); |
| 855 | cp = NULL; |
| 856 | } else { |
| 857 | /* This is the expiration message for the |
| 858 | * connection that was already replaced, so we |
| 859 | * just ignore it. |
| 860 | */ |
| 861 | __ip_vs_conn_put(cp); |
| 862 | kfree(param->pe_data); |
| 863 | return; |
| 864 | } |
| 865 | } |
| 866 | } else { |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 867 | cp = ip_vs_ct_in_get(param); |
Marcelo Ricardo Leitner | d752c36 | 2015-02-23 15:02:34 -0300 | [diff] [blame] | 868 | } |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 869 | |
Julian Anastasov | cdcc5e9 | 2012-04-24 23:46:38 +0300 | [diff] [blame] | 870 | if (cp) { |
| 871 | /* Free pe_data */ |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 872 | kfree(param->pe_data); |
Julian Anastasov | cdcc5e9 | 2012-04-24 23:46:38 +0300 | [diff] [blame] | 873 | |
| 874 | dest = cp->dest; |
Julian Anastasov | ac69269 | 2013-03-22 11:46:54 +0200 | [diff] [blame] | 875 | spin_lock_bh(&cp->lock); |
Julian Anastasov | cdcc5e9 | 2012-04-24 23:46:38 +0300 | [diff] [blame] | 876 | if ((cp->flags ^ flags) & IP_VS_CONN_F_INACTIVE && |
| 877 | !(flags & IP_VS_CONN_F_TEMPLATE) && dest) { |
| 878 | if (flags & IP_VS_CONN_F_INACTIVE) { |
| 879 | atomic_dec(&dest->activeconns); |
| 880 | atomic_inc(&dest->inactconns); |
| 881 | } else { |
| 882 | atomic_inc(&dest->activeconns); |
| 883 | atomic_dec(&dest->inactconns); |
| 884 | } |
| 885 | } |
| 886 | flags &= IP_VS_CONN_F_BACKUP_UPD_MASK; |
| 887 | flags |= cp->flags & ~IP_VS_CONN_F_BACKUP_UPD_MASK; |
| 888 | cp->flags = flags; |
Julian Anastasov | ac69269 | 2013-03-22 11:46:54 +0200 | [diff] [blame] | 889 | spin_unlock_bh(&cp->lock); |
Julian Anastasov | 413c2d04 | 2013-03-22 11:46:52 +0200 | [diff] [blame] | 890 | if (!dest) |
| 891 | ip_vs_try_bind_dest(cp); |
Julian Anastasov | cdcc5e9 | 2012-04-24 23:46:38 +0300 | [diff] [blame] | 892 | } else { |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 893 | /* |
| 894 | * Find the appropriate destination for the connection. |
| 895 | * If it is not found the connection will remain unbound |
| 896 | * but still handled. |
| 897 | */ |
Julian Anastasov | 413c2d04 | 2013-03-22 11:46:52 +0200 | [diff] [blame] | 898 | rcu_read_lock(); |
Alex Gartrell | 655eef1 | 2014-09-09 16:40:21 -0700 | [diff] [blame] | 899 | /* This function is only invoked by the synchronization |
| 900 | * code. We do not currently support heterogeneous pools |
| 901 | * with synchronization, so we can make the assumption that |
| 902 | * the svc_af is the same as the dest_af |
| 903 | */ |
| 904 | dest = ip_vs_find_dest(net, type, type, daddr, dport, |
| 905 | param->vaddr, param->vport, protocol, |
| 906 | fwmark, flags); |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 907 | |
Alex Gartrell | ba38528 | 2014-09-09 16:40:23 -0700 | [diff] [blame] | 908 | cp = ip_vs_conn_new(param, type, daddr, dport, flags, dest, |
| 909 | fwmark); |
Julian Anastasov | 413c2d04 | 2013-03-22 11:46:52 +0200 | [diff] [blame] | 910 | rcu_read_unlock(); |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 911 | if (!cp) { |
Fabian Frederick | b734427 | 2014-06-20 22:35:42 +0200 | [diff] [blame] | 912 | kfree(param->pe_data); |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 913 | IP_VS_DBG(2, "BACKUP, add new conn. failed\n"); |
| 914 | return; |
| 915 | } |
Julian Anastasov | 528c943 | 2015-02-21 21:03:10 +0200 | [diff] [blame] | 916 | if (!(flags & IP_VS_CONN_F_TEMPLATE)) |
| 917 | kfree(param->pe_data); |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | if (opt) |
| 921 | memcpy(&cp->in_seq, opt, sizeof(*opt)); |
Simon Horman | 59e0350 | 2011-02-04 18:33:01 +0900 | [diff] [blame] | 922 | atomic_set(&cp->in_pkts, sysctl_sync_threshold(ipvs)); |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 923 | cp->state = state; |
| 924 | cp->old_state = cp->state; |
| 925 | /* |
| 926 | * For Ver 0 messages style |
| 927 | * - Not possible to recover the right timeout for templates |
| 928 | * - can not find the right fwmark |
| 929 | * virtual service. If needed, we can do it for |
| 930 | * non-fwmark persistent services. |
| 931 | * Ver 1 messages style. |
| 932 | * - No problem. |
| 933 | */ |
| 934 | if (timeout) { |
| 935 | if (timeout > MAX_SCHEDULE_TIMEOUT / HZ) |
| 936 | timeout = MAX_SCHEDULE_TIMEOUT / HZ; |
| 937 | cp->timeout = timeout*HZ; |
Hans Schillstrom | 9bbac6a | 2011-01-03 14:44:52 +0100 | [diff] [blame] | 938 | } else { |
| 939 | struct ip_vs_proto_data *pd; |
| 940 | |
| 941 | pd = ip_vs_proto_data_get(net, protocol); |
| 942 | if (!(flags & IP_VS_CONN_F_TEMPLATE) && pd && pd->timeout_table) |
| 943 | cp->timeout = pd->timeout_table[state]; |
| 944 | else |
| 945 | cp->timeout = (3*60*HZ); |
| 946 | } |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 947 | ip_vs_conn_put(cp); |
| 948 | } |
| 949 | |
| 950 | /* |
| 951 | * Process received multicast message for Version 0 |
| 952 | */ |
Hans Schillstrom | 9bbac6a | 2011-01-03 14:44:52 +0100 | [diff] [blame] | 953 | static void ip_vs_process_message_v0(struct net *net, const char *buffer, |
| 954 | const size_t buflen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | { |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 956 | struct ip_vs_sync_mesg_v0 *m = (struct ip_vs_sync_mesg_v0 *)buffer; |
Hans Schillstrom | 2981bc9 | 2010-11-19 14:25:11 +0100 | [diff] [blame] | 957 | struct ip_vs_sync_conn_v0 *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | struct ip_vs_sync_conn_options *opt; |
Andy Gospodarek | 5c81833 | 2007-10-29 04:35:45 -0700 | [diff] [blame] | 959 | struct ip_vs_protocol *pp; |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 960 | struct ip_vs_conn_param param; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | char *p; |
| 962 | int i; |
| 963 | |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 964 | p = (char *)buffer + sizeof(struct ip_vs_sync_mesg_v0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | for (i=0; i<m->nr_conns; i++) { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 966 | unsigned int flags, state; |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 967 | |
Julian Anastasov | 2ad17de | 2008-04-29 03:21:23 -0700 | [diff] [blame] | 968 | if (p + SIMPLE_CONN_SIZE > buffer+buflen) { |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 969 | IP_VS_ERR_RL("BACKUP v0, bogus conn\n"); |
Julian Anastasov | 2ad17de | 2008-04-29 03:21:23 -0700 | [diff] [blame] | 970 | return; |
| 971 | } |
Hans Schillstrom | 2981bc9 | 2010-11-19 14:25:11 +0100 | [diff] [blame] | 972 | s = (struct ip_vs_sync_conn_v0 *) p; |
Rumen G. Bogdanovski | 7a4fbb1 | 2007-11-19 21:52:42 -0800 | [diff] [blame] | 973 | flags = ntohs(s->flags) | IP_VS_CONN_F_SYNC; |
Julian Anastasov | 2ad17de | 2008-04-29 03:21:23 -0700 | [diff] [blame] | 974 | flags &= ~IP_VS_CONN_F_HASHED; |
| 975 | if (flags & IP_VS_CONN_F_SEQ_MASK) { |
| 976 | opt = (struct ip_vs_sync_conn_options *)&s[1]; |
| 977 | p += FULL_CONN_SIZE; |
| 978 | if (p > buffer+buflen) { |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 979 | IP_VS_ERR_RL("BACKUP v0, Dropping buffer bogus conn options\n"); |
Julian Anastasov | 2ad17de | 2008-04-29 03:21:23 -0700 | [diff] [blame] | 980 | return; |
| 981 | } |
| 982 | } else { |
| 983 | opt = NULL; |
| 984 | p += SIMPLE_CONN_SIZE; |
| 985 | } |
| 986 | |
Rumen G. Bogdanovski | b209639 | 2007-11-19 21:53:27 -0800 | [diff] [blame] | 987 | state = ntohs(s->state); |
Julian Anastasov | 2ad17de | 2008-04-29 03:21:23 -0700 | [diff] [blame] | 988 | if (!(flags & IP_VS_CONN_F_TEMPLATE)) { |
| 989 | pp = ip_vs_proto_get(s->protocol); |
| 990 | if (!pp) { |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 991 | IP_VS_DBG(2, "BACKUP v0, Unsupported protocol %u\n", |
Julian Anastasov | 2ad17de | 2008-04-29 03:21:23 -0700 | [diff] [blame] | 992 | s->protocol); |
| 993 | continue; |
| 994 | } |
| 995 | if (state >= pp->num_states) { |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 996 | IP_VS_DBG(2, "BACKUP v0, Invalid %s state %u\n", |
Julian Anastasov | 2ad17de | 2008-04-29 03:21:23 -0700 | [diff] [blame] | 997 | pp->name, state); |
| 998 | continue; |
| 999 | } |
| 1000 | } else { |
| 1001 | /* protocol in templates is not used for state/timeout */ |
Julian Anastasov | 2ad17de | 2008-04-29 03:21:23 -0700 | [diff] [blame] | 1002 | if (state > 0) { |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1003 | IP_VS_DBG(2, "BACKUP v0, Invalid template state %u\n", |
Julian Anastasov | 2ad17de | 2008-04-29 03:21:23 -0700 | [diff] [blame] | 1004 | state); |
| 1005 | state = 0; |
| 1006 | } |
| 1007 | } |
| 1008 | |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 1009 | ip_vs_conn_fill_param(net, AF_INET, s->protocol, |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1010 | (const union nf_inet_addr *)&s->caddr, |
| 1011 | s->cport, |
| 1012 | (const union nf_inet_addr *)&s->vaddr, |
| 1013 | s->vport, ¶m); |
| 1014 | |
| 1015 | /* Send timeout as Zero */ |
Hans Schillstrom | 9bbac6a | 2011-01-03 14:44:52 +0100 | [diff] [blame] | 1016 | ip_vs_proc_conn(net, ¶m, flags, state, s->protocol, AF_INET, |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1017 | (union nf_inet_addr *)&s->daddr, s->dport, |
Hans Schillstrom | 9bbac6a | 2011-01-03 14:44:52 +0100 | [diff] [blame] | 1018 | 0, 0, opt); |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | /* |
| 1023 | * Handle options |
| 1024 | */ |
| 1025 | static inline int ip_vs_proc_seqopt(__u8 *p, unsigned int plen, |
| 1026 | __u32 *opt_flags, |
| 1027 | struct ip_vs_sync_conn_options *opt) |
| 1028 | { |
| 1029 | struct ip_vs_sync_conn_options *topt; |
| 1030 | |
| 1031 | topt = (struct ip_vs_sync_conn_options *)p; |
| 1032 | |
| 1033 | if (plen != sizeof(struct ip_vs_sync_conn_options)) { |
| 1034 | IP_VS_DBG(2, "BACKUP, bogus conn options length\n"); |
| 1035 | return -EINVAL; |
| 1036 | } |
| 1037 | if (*opt_flags & IPVS_OPT_F_SEQ_DATA) { |
| 1038 | IP_VS_DBG(2, "BACKUP, conn options found twice\n"); |
| 1039 | return -EINVAL; |
| 1040 | } |
| 1041 | ntoh_seq(&topt->in_seq, &opt->in_seq); |
| 1042 | ntoh_seq(&topt->out_seq, &opt->out_seq); |
| 1043 | *opt_flags |= IPVS_OPT_F_SEQ_DATA; |
| 1044 | return 0; |
| 1045 | } |
| 1046 | |
| 1047 | static int ip_vs_proc_str(__u8 *p, unsigned int plen, unsigned int *data_len, |
| 1048 | __u8 **data, unsigned int maxlen, |
| 1049 | __u32 *opt_flags, __u32 flag) |
| 1050 | { |
| 1051 | if (plen > maxlen) { |
| 1052 | IP_VS_DBG(2, "BACKUP, bogus par.data len > %d\n", maxlen); |
| 1053 | return -EINVAL; |
| 1054 | } |
| 1055 | if (*opt_flags & flag) { |
| 1056 | IP_VS_DBG(2, "BACKUP, Par.data found twice 0x%x\n", flag); |
| 1057 | return -EINVAL; |
| 1058 | } |
| 1059 | *data_len = plen; |
| 1060 | *data = p; |
| 1061 | *opt_flags |= flag; |
| 1062 | return 0; |
| 1063 | } |
| 1064 | /* |
| 1065 | * Process a Version 1 sync. connection |
| 1066 | */ |
Hans Schillstrom | 9bbac6a | 2011-01-03 14:44:52 +0100 | [diff] [blame] | 1067 | static inline int ip_vs_proc_sync_conn(struct net *net, __u8 *p, __u8 *msg_end) |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1068 | { |
| 1069 | struct ip_vs_sync_conn_options opt; |
| 1070 | union ip_vs_sync_conn *s; |
| 1071 | struct ip_vs_protocol *pp; |
| 1072 | struct ip_vs_conn_param param; |
| 1073 | __u32 flags; |
| 1074 | unsigned int af, state, pe_data_len=0, pe_name_len=0; |
| 1075 | __u8 *pe_data=NULL, *pe_name=NULL; |
| 1076 | __u32 opt_flags=0; |
| 1077 | int retc=0; |
| 1078 | |
| 1079 | s = (union ip_vs_sync_conn *) p; |
| 1080 | |
| 1081 | if (s->v6.type & STYPE_F_INET6) { |
| 1082 | #ifdef CONFIG_IP_VS_IPV6 |
| 1083 | af = AF_INET6; |
| 1084 | p += sizeof(struct ip_vs_sync_v6); |
| 1085 | #else |
| 1086 | IP_VS_DBG(3,"BACKUP, IPv6 msg received, and IPVS is not compiled for IPv6\n"); |
| 1087 | retc = 10; |
| 1088 | goto out; |
| 1089 | #endif |
| 1090 | } else if (!s->v4.type) { |
| 1091 | af = AF_INET; |
| 1092 | p += sizeof(struct ip_vs_sync_v4); |
| 1093 | } else { |
| 1094 | return -10; |
| 1095 | } |
| 1096 | if (p > msg_end) |
| 1097 | return -20; |
| 1098 | |
| 1099 | /* Process optional params check Type & Len. */ |
| 1100 | while (p < msg_end) { |
| 1101 | int ptype; |
| 1102 | int plen; |
| 1103 | |
| 1104 | if (p+2 > msg_end) |
| 1105 | return -30; |
| 1106 | ptype = *(p++); |
| 1107 | plen = *(p++); |
| 1108 | |
| 1109 | if (!plen || ((p + plen) > msg_end)) |
| 1110 | return -40; |
| 1111 | /* Handle seq option p = param data */ |
| 1112 | switch (ptype & ~IPVS_OPT_F_PARAM) { |
| 1113 | case IPVS_OPT_SEQ_DATA: |
| 1114 | if (ip_vs_proc_seqopt(p, plen, &opt_flags, &opt)) |
| 1115 | return -50; |
| 1116 | break; |
| 1117 | |
| 1118 | case IPVS_OPT_PE_DATA: |
| 1119 | if (ip_vs_proc_str(p, plen, &pe_data_len, &pe_data, |
| 1120 | IP_VS_PEDATA_MAXLEN, &opt_flags, |
| 1121 | IPVS_OPT_F_PE_DATA)) |
| 1122 | return -60; |
| 1123 | break; |
| 1124 | |
| 1125 | case IPVS_OPT_PE_NAME: |
| 1126 | if (ip_vs_proc_str(p, plen,&pe_name_len, &pe_name, |
| 1127 | IP_VS_PENAME_MAXLEN, &opt_flags, |
| 1128 | IPVS_OPT_F_PE_NAME)) |
| 1129 | return -70; |
| 1130 | break; |
| 1131 | |
| 1132 | default: |
| 1133 | /* Param data mandatory ? */ |
| 1134 | if (!(ptype & IPVS_OPT_F_PARAM)) { |
| 1135 | IP_VS_DBG(3, "BACKUP, Unknown mandatory param %d found\n", |
| 1136 | ptype & ~IPVS_OPT_F_PARAM); |
| 1137 | retc = 20; |
| 1138 | goto out; |
Rumen G. Bogdanovski | b209639 | 2007-11-19 21:53:27 -0800 | [diff] [blame] | 1139 | } |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1140 | } |
| 1141 | p += plen; /* Next option */ |
| 1142 | } |
| 1143 | |
| 1144 | /* Get flags and Mask off unsupported */ |
| 1145 | flags = ntohl(s->v4.flags) & IP_VS_CONN_F_BACKUP_MASK; |
| 1146 | flags |= IP_VS_CONN_F_SYNC; |
| 1147 | state = ntohs(s->v4.state); |
| 1148 | |
| 1149 | if (!(flags & IP_VS_CONN_F_TEMPLATE)) { |
| 1150 | pp = ip_vs_proto_get(s->v4.protocol); |
| 1151 | if (!pp) { |
| 1152 | IP_VS_DBG(3,"BACKUP, Unsupported protocol %u\n", |
| 1153 | s->v4.protocol); |
| 1154 | retc = 30; |
| 1155 | goto out; |
| 1156 | } |
| 1157 | if (state >= pp->num_states) { |
| 1158 | IP_VS_DBG(3, "BACKUP, Invalid %s state %u\n", |
| 1159 | pp->name, state); |
| 1160 | retc = 40; |
| 1161 | goto out; |
| 1162 | } |
| 1163 | } else { |
| 1164 | /* protocol in templates is not used for state/timeout */ |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1165 | if (state > 0) { |
| 1166 | IP_VS_DBG(3, "BACKUP, Invalid template state %u\n", |
| 1167 | state); |
| 1168 | state = 0; |
| 1169 | } |
| 1170 | } |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 1171 | if (ip_vs_conn_fill_param_sync(net, af, s, ¶m, pe_data, |
| 1172 | pe_data_len, pe_name, pe_name_len)) { |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1173 | retc = 50; |
| 1174 | goto out; |
| 1175 | } |
| 1176 | /* If only IPv4, just silent skip IPv6 */ |
| 1177 | if (af == AF_INET) |
Hans Schillstrom | 9bbac6a | 2011-01-03 14:44:52 +0100 | [diff] [blame] | 1178 | ip_vs_proc_conn(net, ¶m, flags, state, s->v4.protocol, af, |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1179 | (union nf_inet_addr *)&s->v4.daddr, s->v4.dport, |
| 1180 | ntohl(s->v4.timeout), ntohl(s->v4.fwmark), |
Hans Schillstrom | 9bbac6a | 2011-01-03 14:44:52 +0100 | [diff] [blame] | 1181 | (opt_flags & IPVS_OPT_F_SEQ_DATA ? &opt : NULL) |
| 1182 | ); |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1183 | #ifdef CONFIG_IP_VS_IPV6 |
| 1184 | else |
Hans Schillstrom | 9bbac6a | 2011-01-03 14:44:52 +0100 | [diff] [blame] | 1185 | ip_vs_proc_conn(net, ¶m, flags, state, s->v6.protocol, af, |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1186 | (union nf_inet_addr *)&s->v6.daddr, s->v6.dport, |
| 1187 | ntohl(s->v6.timeout), ntohl(s->v6.fwmark), |
Hans Schillstrom | 9bbac6a | 2011-01-03 14:44:52 +0100 | [diff] [blame] | 1188 | (opt_flags & IPVS_OPT_F_SEQ_DATA ? &opt : NULL) |
| 1189 | ); |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1190 | #endif |
Julian Anastasov | 528c943 | 2015-02-21 21:03:10 +0200 | [diff] [blame] | 1191 | ip_vs_pe_put(param.pe); |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1192 | return 0; |
| 1193 | /* Error exit */ |
| 1194 | out: |
| 1195 | IP_VS_DBG(2, "BACKUP, Single msg dropped err:%d\n", retc); |
| 1196 | return retc; |
| 1197 | |
| 1198 | } |
| 1199 | /* |
| 1200 | * Process received multicast message and create the corresponding |
| 1201 | * ip_vs_conn entries. |
| 1202 | * Handles Version 0 & 1 |
| 1203 | */ |
Hans Schillstrom | 9bbac6a | 2011-01-03 14:44:52 +0100 | [diff] [blame] | 1204 | static void ip_vs_process_message(struct net *net, __u8 *buffer, |
| 1205 | const size_t buflen) |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1206 | { |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1207 | struct netns_ipvs *ipvs = net_ipvs(net); |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 1208 | struct ip_vs_sync_mesg *m2 = (struct ip_vs_sync_mesg *)buffer; |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1209 | __u8 *p, *msg_end; |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 1210 | int i, nr_conns; |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1211 | |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 1212 | if (buflen < sizeof(struct ip_vs_sync_mesg_v0)) { |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1213 | IP_VS_DBG(2, "BACKUP, message header too short\n"); |
| 1214 | return; |
| 1215 | } |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1216 | |
Simon Horman | 3856143 | 2013-04-19 10:43:44 +0900 | [diff] [blame] | 1217 | if (buflen != ntohs(m2->size)) { |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1218 | IP_VS_DBG(2, "BACKUP, bogus message size\n"); |
| 1219 | return; |
| 1220 | } |
| 1221 | /* SyncID sanity check */ |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1222 | if (ipvs->backup_syncid != 0 && m2->syncid != ipvs->backup_syncid) { |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1223 | IP_VS_DBG(7, "BACKUP, Ignoring syncid = %d\n", m2->syncid); |
| 1224 | return; |
| 1225 | } |
| 1226 | /* Handle version 1 message */ |
| 1227 | if ((m2->version == SYNC_PROTO_VER) && (m2->reserved == 0) |
| 1228 | && (m2->spare == 0)) { |
| 1229 | |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 1230 | msg_end = buffer + sizeof(struct ip_vs_sync_mesg); |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1231 | nr_conns = m2->nr_conns; |
| 1232 | |
| 1233 | for (i=0; i<nr_conns; i++) { |
| 1234 | union ip_vs_sync_conn *s; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1235 | unsigned int size; |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1236 | int retc; |
| 1237 | |
| 1238 | p = msg_end; |
| 1239 | if (p + sizeof(s->v4) > buffer+buflen) { |
| 1240 | IP_VS_ERR_RL("BACKUP, Dropping buffer, to small\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | return; |
| 1242 | } |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1243 | s = (union ip_vs_sync_conn *)p; |
| 1244 | size = ntohs(s->v4.ver_size) & SVER_MASK; |
| 1245 | msg_end = p + size; |
| 1246 | /* Basic sanity checks */ |
| 1247 | if (msg_end > buffer+buflen) { |
| 1248 | IP_VS_ERR_RL("BACKUP, Dropping buffer, msg > buffer\n"); |
| 1249 | return; |
Rumen G. Bogdanovski | b209639 | 2007-11-19 21:53:27 -0800 | [diff] [blame] | 1250 | } |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1251 | if (ntohs(s->v4.ver_size) >> SVER_SHIFT) { |
| 1252 | IP_VS_ERR_RL("BACKUP, Dropping buffer, Unknown version %d\n", |
| 1253 | ntohs(s->v4.ver_size) >> SVER_SHIFT); |
| 1254 | return; |
Venkata Mohan Reddy | 2906f66 | 2010-02-18 12:31:05 +0100 | [diff] [blame] | 1255 | } |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1256 | /* Process a single sync_conn */ |
Hans Schillstrom | 9bbac6a | 2011-01-03 14:44:52 +0100 | [diff] [blame] | 1257 | retc = ip_vs_proc_sync_conn(net, p, msg_end); |
| 1258 | if (retc < 0) { |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1259 | IP_VS_ERR_RL("BACKUP, Dropping buffer, Err: %d in decoding\n", |
| 1260 | retc); |
| 1261 | return; |
| 1262 | } |
| 1263 | /* Make sure we have 32 bit alignment */ |
| 1264 | msg_end = p + ((size + 3) & ~3); |
Rumen G. Bogdanovski | b209639 | 2007-11-19 21:53:27 -0800 | [diff] [blame] | 1265 | } |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1266 | } else { |
| 1267 | /* Old type of message */ |
Hans Schillstrom | 9bbac6a | 2011-01-03 14:44:52 +0100 | [diff] [blame] | 1268 | ip_vs_process_message_v0(net, buffer, buflen); |
Hans Schillstrom | fe5e7a1 | 2010-11-19 14:25:12 +0100 | [diff] [blame] | 1269 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | |
| 1274 | /* |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 1275 | * Setup sndbuf (mode=1) or rcvbuf (mode=0) |
| 1276 | */ |
| 1277 | static void set_sock_size(struct sock *sk, int mode, int val) |
| 1278 | { |
| 1279 | /* setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &val, sizeof(val)); */ |
| 1280 | /* setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)); */ |
| 1281 | lock_sock(sk); |
| 1282 | if (mode) { |
| 1283 | val = clamp_t(int, val, (SOCK_MIN_SNDBUF + 1) / 2, |
| 1284 | sysctl_wmem_max); |
| 1285 | sk->sk_sndbuf = val * 2; |
| 1286 | sk->sk_userlocks |= SOCK_SNDBUF_LOCK; |
| 1287 | } else { |
| 1288 | val = clamp_t(int, val, (SOCK_MIN_RCVBUF + 1) / 2, |
| 1289 | sysctl_rmem_max); |
| 1290 | sk->sk_rcvbuf = val * 2; |
| 1291 | sk->sk_userlocks |= SOCK_RCVBUF_LOCK; |
| 1292 | } |
| 1293 | release_sock(sk); |
| 1294 | } |
| 1295 | |
| 1296 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | * Setup loopback of outgoing multicasts on a sending socket |
| 1298 | */ |
| 1299 | static void set_mcast_loop(struct sock *sk, u_char loop) |
| 1300 | { |
| 1301 | struct inet_sock *inet = inet_sk(sk); |
| 1302 | |
| 1303 | /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */ |
| 1304 | lock_sock(sk); |
| 1305 | inet->mc_loop = loop ? 1 : 0; |
| 1306 | release_sock(sk); |
| 1307 | } |
| 1308 | |
| 1309 | /* |
| 1310 | * Specify TTL for outgoing multicasts on a sending socket |
| 1311 | */ |
| 1312 | static void set_mcast_ttl(struct sock *sk, u_char ttl) |
| 1313 | { |
| 1314 | struct inet_sock *inet = inet_sk(sk); |
| 1315 | |
| 1316 | /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */ |
| 1317 | lock_sock(sk); |
| 1318 | inet->mc_ttl = ttl; |
| 1319 | release_sock(sk); |
| 1320 | } |
| 1321 | |
| 1322 | /* |
| 1323 | * Specifiy default interface for outgoing multicasts |
| 1324 | */ |
| 1325 | static int set_mcast_if(struct sock *sk, char *ifname) |
| 1326 | { |
| 1327 | struct net_device *dev; |
| 1328 | struct inet_sock *inet = inet_sk(sk); |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1329 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1331 | dev = __dev_get_by_name(net, ifname); |
| 1332 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | return -ENODEV; |
| 1334 | |
| 1335 | if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if) |
| 1336 | return -EINVAL; |
| 1337 | |
| 1338 | lock_sock(sk); |
| 1339 | inet->mc_index = dev->ifindex; |
| 1340 | /* inet->mc_addr = 0; */ |
| 1341 | release_sock(sk); |
| 1342 | |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
| 1346 | |
| 1347 | /* |
| 1348 | * Set the maximum length of sync message according to the |
| 1349 | * specified interface's MTU. |
| 1350 | */ |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1351 | static int set_sync_mesg_maxlen(struct net *net, int sync_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | { |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1353 | struct netns_ipvs *ipvs = net_ipvs(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | struct net_device *dev; |
| 1355 | int num; |
| 1356 | |
| 1357 | if (sync_state == IP_VS_STATE_MASTER) { |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1358 | dev = __dev_get_by_name(net, ipvs->master_mcast_ifn); |
| 1359 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | return -ENODEV; |
| 1361 | |
| 1362 | num = (dev->mtu - sizeof(struct iphdr) - |
| 1363 | sizeof(struct udphdr) - |
| 1364 | SYNC_MESG_HEADER_LEN - 20) / SIMPLE_CONN_SIZE; |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1365 | ipvs->send_mesg_maxlen = SYNC_MESG_HEADER_LEN + |
Sven Wegener | e6f225e | 2008-09-19 20:41:56 +0200 | [diff] [blame] | 1366 | SIMPLE_CONN_SIZE * min(num, MAX_CONNS_PER_SYNCBUFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | IP_VS_DBG(7, "setting the maximum length of sync sending " |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1368 | "message %d.\n", ipvs->send_mesg_maxlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | } else if (sync_state == IP_VS_STATE_BACKUP) { |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1370 | dev = __dev_get_by_name(net, ipvs->backup_mcast_ifn); |
| 1371 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | return -ENODEV; |
| 1373 | |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1374 | ipvs->recv_mesg_maxlen = dev->mtu - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | sizeof(struct iphdr) - sizeof(struct udphdr); |
| 1376 | IP_VS_DBG(7, "setting the maximum length of sync receiving " |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1377 | "message %d.\n", ipvs->recv_mesg_maxlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | return 0; |
| 1381 | } |
| 1382 | |
| 1383 | |
| 1384 | /* |
| 1385 | * Join a multicast group. |
| 1386 | * the group is specified by a class D multicast address 224.0.0.0/8 |
| 1387 | * in the in_addr structure passed in as a parameter. |
| 1388 | */ |
| 1389 | static int |
| 1390 | join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname) |
| 1391 | { |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1392 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | struct ip_mreqn mreq; |
| 1394 | struct net_device *dev; |
| 1395 | int ret; |
| 1396 | |
| 1397 | memset(&mreq, 0, sizeof(mreq)); |
| 1398 | memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr)); |
| 1399 | |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1400 | dev = __dev_get_by_name(net, ifname); |
| 1401 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | return -ENODEV; |
| 1403 | if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if) |
| 1404 | return -EINVAL; |
| 1405 | |
| 1406 | mreq.imr_ifindex = dev->ifindex; |
| 1407 | |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 1408 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | lock_sock(sk); |
| 1410 | ret = ip_mc_join_group(sk, &mreq); |
| 1411 | release_sock(sk); |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 1412 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | |
| 1414 | return ret; |
| 1415 | } |
| 1416 | |
| 1417 | |
| 1418 | static int bind_mcastif_addr(struct socket *sock, char *ifname) |
| 1419 | { |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1420 | struct net *net = sock_net(sock->sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | struct net_device *dev; |
Al Viro | a61ced5 | 2006-09-26 21:27:54 -0700 | [diff] [blame] | 1422 | __be32 addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | struct sockaddr_in sin; |
| 1424 | |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1425 | dev = __dev_get_by_name(net, ifname); |
| 1426 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | return -ENODEV; |
| 1428 | |
| 1429 | addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE); |
| 1430 | if (!addr) |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1431 | pr_err("You probably need to specify IP address on " |
| 1432 | "multicast interface.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | |
Harvey Harrison | 14d5e83 | 2008-10-31 00:54:29 -0700 | [diff] [blame] | 1434 | IP_VS_DBG(7, "binding socket with (%s) %pI4\n", |
| 1435 | ifname, &addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | |
| 1437 | /* Now bind the socket with the address of multicast interface */ |
| 1438 | sin.sin_family = AF_INET; |
| 1439 | sin.sin_addr.s_addr = addr; |
| 1440 | sin.sin_port = 0; |
| 1441 | |
| 1442 | return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin)); |
| 1443 | } |
| 1444 | |
| 1445 | /* |
| 1446 | * Set up sending multicast socket over UDP |
| 1447 | */ |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1448 | static struct socket *make_send_sock(struct net *net, int id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | { |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1450 | struct netns_ipvs *ipvs = net_ipvs(net); |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1451 | /* multicast addr */ |
| 1452 | struct sockaddr_in mcast_addr = { |
| 1453 | .sin_family = AF_INET, |
| 1454 | .sin_port = cpu_to_be16(IP_VS_SYNC_PORT + id), |
| 1455 | .sin_addr.s_addr = cpu_to_be32(IP_VS_SYNC_GROUP), |
| 1456 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | struct socket *sock; |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 1458 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | |
Hans Schillstrom | 1ae132b | 2011-05-03 22:09:30 +0200 | [diff] [blame] | 1460 | /* First create a socket move it to right name space later */ |
Eric W. Biederman | eeb1bd5 | 2015-05-08 21:08:05 -0500 | [diff] [blame^] | 1461 | result = sock_create_kern(&init_net, PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock); |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 1462 | if (result < 0) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1463 | pr_err("Error during creation of socket; terminating\n"); |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 1464 | return ERR_PTR(result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | } |
Hans Schillstrom | 1ae132b | 2011-05-03 22:09:30 +0200 | [diff] [blame] | 1466 | /* |
| 1467 | * Kernel sockets that are a part of a namespace, should not |
| 1468 | * hold a reference to a namespace in order to allow to stop it. |
| 1469 | * After sk_change_net should be released using sk_release_kernel. |
| 1470 | */ |
| 1471 | sk_change_net(sock->sk, net); |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1472 | result = set_mcast_if(sock->sk, ipvs->master_mcast_ifn); |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 1473 | if (result < 0) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1474 | pr_err("Error setting outbound mcast interface\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | goto error; |
| 1476 | } |
| 1477 | |
| 1478 | set_mcast_loop(sock->sk, 0); |
| 1479 | set_mcast_ttl(sock->sk, 1); |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 1480 | result = sysctl_sync_sock_size(ipvs); |
| 1481 | if (result > 0) |
| 1482 | set_sock_size(sock->sk, 1, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1484 | result = bind_mcastif_addr(sock, ipvs->master_mcast_ifn); |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 1485 | if (result < 0) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1486 | pr_err("Error binding address of the mcast interface\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | goto error; |
| 1488 | } |
| 1489 | |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 1490 | result = sock->ops->connect(sock, (struct sockaddr *) &mcast_addr, |
| 1491 | sizeof(struct sockaddr), 0); |
| 1492 | if (result < 0) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1493 | pr_err("Error connecting to the multicast addr\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | goto error; |
| 1495 | } |
| 1496 | |
| 1497 | return sock; |
| 1498 | |
Hans Schillstrom | 1ae132b | 2011-05-03 22:09:30 +0200 | [diff] [blame] | 1499 | error: |
| 1500 | sk_release_kernel(sock->sk); |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 1501 | return ERR_PTR(result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | |
| 1505 | /* |
| 1506 | * Set up receiving multicast socket over UDP |
| 1507 | */ |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1508 | static struct socket *make_receive_sock(struct net *net, int id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | { |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1510 | struct netns_ipvs *ipvs = net_ipvs(net); |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1511 | /* multicast addr */ |
| 1512 | struct sockaddr_in mcast_addr = { |
| 1513 | .sin_family = AF_INET, |
| 1514 | .sin_port = cpu_to_be16(IP_VS_SYNC_PORT + id), |
| 1515 | .sin_addr.s_addr = cpu_to_be32(IP_VS_SYNC_GROUP), |
| 1516 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | struct socket *sock; |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 1518 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | |
| 1520 | /* First create a socket */ |
Eric W. Biederman | eeb1bd5 | 2015-05-08 21:08:05 -0500 | [diff] [blame^] | 1521 | result = sock_create_kern(&init_net, PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock); |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 1522 | if (result < 0) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1523 | pr_err("Error during creation of socket; terminating\n"); |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 1524 | return ERR_PTR(result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | } |
Hans Schillstrom | 1ae132b | 2011-05-03 22:09:30 +0200 | [diff] [blame] | 1526 | /* |
| 1527 | * Kernel sockets that are a part of a namespace, should not |
| 1528 | * hold a reference to a namespace in order to allow to stop it. |
| 1529 | * After sk_change_net should be released using sk_release_kernel. |
| 1530 | */ |
| 1531 | sk_change_net(sock->sk, net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | /* it is equivalent to the REUSEADDR option in user-space */ |
Pavel Emelyanov | 4a17fd5 | 2012-04-19 03:39:36 +0000 | [diff] [blame] | 1533 | sock->sk->sk_reuse = SK_CAN_REUSE; |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 1534 | result = sysctl_sync_sock_size(ipvs); |
| 1535 | if (result > 0) |
| 1536 | set_sock_size(sock->sk, 0, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 1538 | result = sock->ops->bind(sock, (struct sockaddr *) &mcast_addr, |
| 1539 | sizeof(struct sockaddr)); |
| 1540 | if (result < 0) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1541 | pr_err("Error binding to the multicast addr\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | goto error; |
| 1543 | } |
| 1544 | |
| 1545 | /* join the multicast group */ |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 1546 | result = join_mcast_group(sock->sk, |
| 1547 | (struct in_addr *) &mcast_addr.sin_addr, |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1548 | ipvs->backup_mcast_ifn); |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 1549 | if (result < 0) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1550 | pr_err("Error joining to the multicast group\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | goto error; |
| 1552 | } |
| 1553 | |
| 1554 | return sock; |
| 1555 | |
Hans Schillstrom | 1ae132b | 2011-05-03 22:09:30 +0200 | [diff] [blame] | 1556 | error: |
| 1557 | sk_release_kernel(sock->sk); |
Sven Wegener | e6dd731 | 2008-07-16 11:13:43 +0000 | [diff] [blame] | 1558 | return ERR_PTR(result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | |
| 1562 | static int |
| 1563 | ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length) |
| 1564 | { |
| 1565 | struct msghdr msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL}; |
| 1566 | struct kvec iov; |
| 1567 | int len; |
| 1568 | |
| 1569 | EnterFunction(7); |
| 1570 | iov.iov_base = (void *)buffer; |
| 1571 | iov.iov_len = length; |
| 1572 | |
| 1573 | len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length)); |
| 1574 | |
| 1575 | LeaveFunction(7); |
| 1576 | return len; |
| 1577 | } |
| 1578 | |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 1579 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg) |
| 1581 | { |
| 1582 | int msize; |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 1583 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | |
Simon Horman | 3856143 | 2013-04-19 10:43:44 +0900 | [diff] [blame] | 1585 | msize = ntohs(msg->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 1587 | ret = ip_vs_send_async(sock, (char *)msg, msize); |
| 1588 | if (ret >= 0 || ret == -EAGAIN) |
| 1589 | return ret; |
| 1590 | pr_err("ip_vs_send_async error %d\n", ret); |
| 1591 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | static int |
| 1595 | ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen) |
| 1596 | { |
| 1597 | struct msghdr msg = {NULL,}; |
| 1598 | struct kvec iov; |
| 1599 | int len; |
| 1600 | |
| 1601 | EnterFunction(7); |
| 1602 | |
| 1603 | /* Receive a packet */ |
| 1604 | iov.iov_base = buffer; |
| 1605 | iov.iov_len = (size_t)buflen; |
| 1606 | |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1607 | len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, MSG_DONTWAIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | |
| 1609 | if (len < 0) |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1610 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | |
| 1612 | LeaveFunction(7); |
| 1613 | return len; |
| 1614 | } |
| 1615 | |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 1616 | /* Wakeup the master thread for sending */ |
| 1617 | static void master_wakeup_work_handler(struct work_struct *work) |
| 1618 | { |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1619 | struct ipvs_master_sync_state *ms = |
| 1620 | container_of(work, struct ipvs_master_sync_state, |
| 1621 | master_wakeup_work.work); |
| 1622 | struct netns_ipvs *ipvs = ms->ipvs; |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 1623 | |
| 1624 | spin_lock_bh(&ipvs->sync_lock); |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1625 | if (ms->sync_queue_len && |
| 1626 | ms->sync_queue_delay < IPVS_SYNC_WAKEUP_RATE) { |
| 1627 | ms->sync_queue_delay = IPVS_SYNC_WAKEUP_RATE; |
| 1628 | wake_up_process(ms->master_thread); |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 1629 | } |
| 1630 | spin_unlock_bh(&ipvs->sync_lock); |
| 1631 | } |
| 1632 | |
| 1633 | /* Get next buffer to send */ |
| 1634 | static inline struct ip_vs_sync_buff * |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1635 | next_sync_buff(struct netns_ipvs *ipvs, struct ipvs_master_sync_state *ms) |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 1636 | { |
| 1637 | struct ip_vs_sync_buff *sb; |
| 1638 | |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1639 | sb = sb_dequeue(ipvs, ms); |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 1640 | if (sb) |
| 1641 | return sb; |
| 1642 | /* Do not delay entries in buffer for more than 2 seconds */ |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1643 | return get_curr_sync_buff(ipvs, ms, IPVS_SYNC_FLUSH_TIME); |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 1644 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1646 | static int sync_thread_master(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | { |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1648 | struct ip_vs_sync_thread_data *tinfo = data; |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1649 | struct netns_ipvs *ipvs = net_ipvs(tinfo->net); |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1650 | struct ipvs_master_sync_state *ms = &ipvs->ms[tinfo->id]; |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 1651 | struct sock *sk = tinfo->sock->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | struct ip_vs_sync_buff *sb; |
| 1653 | |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1654 | pr_info("sync thread started: state = MASTER, mcast_ifn = %s, " |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1655 | "syncid = %d, id = %d\n", |
| 1656 | ipvs->master_mcast_ifn, ipvs->master_syncid, tinfo->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 1658 | for (;;) { |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1659 | sb = next_sync_buff(ipvs, ms); |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 1660 | if (unlikely(kthread_should_stop())) |
| 1661 | break; |
| 1662 | if (!sb) { |
| 1663 | schedule_timeout(IPVS_SYNC_CHECK_PERIOD); |
| 1664 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | } |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 1666 | while (ip_vs_send_sync_msg(tinfo->sock, sb->mesg) < 0) { |
Geert Uytterhoeven | 9dcbe1b | 2013-11-12 15:34:24 +0100 | [diff] [blame] | 1667 | /* (Ab)use interruptible sleep to avoid increasing |
| 1668 | * the load avg. |
| 1669 | */ |
| 1670 | __wait_event_interruptible(*sk_sleep(sk), |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 1671 | sock_writeable(sk) || |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 1672 | kthread_should_stop()); |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 1673 | if (unlikely(kthread_should_stop())) |
| 1674 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | } |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 1676 | ip_vs_sync_buff_release(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | } |
| 1678 | |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 1679 | done: |
| 1680 | __set_current_state(TASK_RUNNING); |
| 1681 | if (sb) |
| 1682 | ip_vs_sync_buff_release(sb); |
| 1683 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | /* clean up the sync_buff queue */ |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1685 | while ((sb = sb_dequeue(ipvs, ms))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | ip_vs_sync_buff_release(sb); |
Pablo Neira Ayuso | 1c003b1 | 2012-05-08 19:39:49 +0200 | [diff] [blame] | 1687 | __set_current_state(TASK_RUNNING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | |
| 1689 | /* clean up the current sync_buff */ |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1690 | sb = get_curr_sync_buff(ipvs, ms, 0); |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1691 | if (sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | ip_vs_sync_buff_release(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | |
| 1694 | /* release the sending multicast socket */ |
Hans Schillstrom | 1ae132b | 2011-05-03 22:09:30 +0200 | [diff] [blame] | 1695 | sk_release_kernel(tinfo->sock->sk); |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1696 | kfree(tinfo); |
| 1697 | |
| 1698 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1702 | static int sync_thread_backup(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | { |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1704 | struct ip_vs_sync_thread_data *tinfo = data; |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1705 | struct netns_ipvs *ipvs = net_ipvs(tinfo->net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | int len; |
| 1707 | |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1708 | pr_info("sync thread started: state = BACKUP, mcast_ifn = %s, " |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1709 | "syncid = %d, id = %d\n", |
| 1710 | ipvs->backup_mcast_ifn, ipvs->backup_syncid, tinfo->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1712 | while (!kthread_should_stop()) { |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 1713 | wait_event_interruptible(*sk_sleep(tinfo->sock->sk), |
Sven Wegener | ba6fd85 | 2008-07-16 11:13:56 +0000 | [diff] [blame] | 1714 | !skb_queue_empty(&tinfo->sock->sk->sk_receive_queue) |
| 1715 | || kthread_should_stop()); |
| 1716 | |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1717 | /* do we have data now? */ |
| 1718 | while (!skb_queue_empty(&(tinfo->sock->sk->sk_receive_queue))) { |
| 1719 | len = ip_vs_receive(tinfo->sock, tinfo->buf, |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1720 | ipvs->recv_mesg_maxlen); |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1721 | if (len <= 0) { |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1722 | if (len != -EAGAIN) |
| 1723 | pr_err("receiving message error\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | break; |
| 1725 | } |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1726 | |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1727 | ip_vs_process_message(tinfo->net, tinfo->buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | /* release the sending multicast socket */ |
Hans Schillstrom | 1ae132b | 2011-05-03 22:09:30 +0200 | [diff] [blame] | 1732 | sk_release_kernel(tinfo->sock->sk); |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1733 | kfree(tinfo->buf); |
Neil Horman | cc0191a | 2007-06-18 22:33:20 -0700 | [diff] [blame] | 1734 | kfree(tinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | |
| 1736 | return 0; |
| 1737 | } |
| 1738 | |
| 1739 | |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1740 | int start_sync_thread(struct net *net, int state, char *mcast_ifn, __u8 syncid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | { |
Neil Horman | cc0191a | 2007-06-18 22:33:20 -0700 | [diff] [blame] | 1742 | struct ip_vs_sync_thread_data *tinfo; |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1743 | struct task_struct **array = NULL, *task; |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1744 | struct socket *sock; |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1745 | struct netns_ipvs *ipvs = net_ipvs(net); |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1746 | char *name; |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1747 | int (*threadfn)(void *data); |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1748 | int id, count; |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1749 | int result = -ENOMEM; |
Neil Horman | cc0191a | 2007-06-18 22:33:20 -0700 | [diff] [blame] | 1750 | |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1751 | IP_VS_DBG(7, "%s(): pid %d\n", __func__, task_pid_nr(current)); |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1752 | IP_VS_DBG(7, "Each ip_vs_sync_conn entry needs %Zd bytes\n", |
Hans Schillstrom | 2981bc9 | 2010-11-19 14:25:11 +0100 | [diff] [blame] | 1753 | sizeof(struct ip_vs_sync_conn_v0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1755 | if (!ipvs->sync_state) { |
| 1756 | count = clamp(sysctl_sync_ports(ipvs), 1, IPVS_SYNC_PORTS_MAX); |
| 1757 | ipvs->threads_mask = count - 1; |
| 1758 | } else |
| 1759 | count = ipvs->threads_mask + 1; |
Hans Schillstrom | ae1d48b | 2011-10-11 10:54:35 +0900 | [diff] [blame] | 1760 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | if (state == IP_VS_STATE_MASTER) { |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1762 | if (ipvs->ms) |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1763 | return -EEXIST; |
| 1764 | |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1765 | strlcpy(ipvs->master_mcast_ifn, mcast_ifn, |
| 1766 | sizeof(ipvs->master_mcast_ifn)); |
| 1767 | ipvs->master_syncid = syncid; |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1768 | name = "ipvs-m:%d:%d"; |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1769 | threadfn = sync_thread_master; |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1770 | } else if (state == IP_VS_STATE_BACKUP) { |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1771 | if (ipvs->backup_threads) |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1772 | return -EEXIST; |
| 1773 | |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1774 | strlcpy(ipvs->backup_mcast_ifn, mcast_ifn, |
| 1775 | sizeof(ipvs->backup_mcast_ifn)); |
| 1776 | ipvs->backup_syncid = syncid; |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1777 | name = "ipvs-b:%d:%d"; |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1778 | threadfn = sync_thread_backup; |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1779 | } else { |
| 1780 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | } |
| 1782 | |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1783 | if (state == IP_VS_STATE_MASTER) { |
| 1784 | struct ipvs_master_sync_state *ms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1786 | ipvs->ms = kzalloc(count * sizeof(ipvs->ms[0]), GFP_KERNEL); |
| 1787 | if (!ipvs->ms) |
| 1788 | goto out; |
| 1789 | ms = ipvs->ms; |
| 1790 | for (id = 0; id < count; id++, ms++) { |
| 1791 | INIT_LIST_HEAD(&ms->sync_queue); |
| 1792 | ms->sync_queue_len = 0; |
| 1793 | ms->sync_queue_delay = 0; |
| 1794 | INIT_DELAYED_WORK(&ms->master_wakeup_work, |
| 1795 | master_wakeup_work_handler); |
| 1796 | ms->ipvs = ipvs; |
| 1797 | } |
| 1798 | } else { |
| 1799 | array = kzalloc(count * sizeof(struct task_struct *), |
| 1800 | GFP_KERNEL); |
| 1801 | if (!array) |
| 1802 | goto out; |
| 1803 | } |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1804 | set_sync_mesg_maxlen(net, state); |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1805 | |
| 1806 | tinfo = NULL; |
| 1807 | for (id = 0; id < count; id++) { |
| 1808 | if (state == IP_VS_STATE_MASTER) |
| 1809 | sock = make_send_sock(net, id); |
| 1810 | else |
| 1811 | sock = make_receive_sock(net, id); |
| 1812 | if (IS_ERR(sock)) { |
| 1813 | result = PTR_ERR(sock); |
| 1814 | goto outtinfo; |
| 1815 | } |
| 1816 | tinfo = kmalloc(sizeof(*tinfo), GFP_KERNEL); |
| 1817 | if (!tinfo) |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1818 | goto outsocket; |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1819 | tinfo->net = net; |
| 1820 | tinfo->sock = sock; |
| 1821 | if (state == IP_VS_STATE_BACKUP) { |
| 1822 | tinfo->buf = kmalloc(ipvs->recv_mesg_maxlen, |
| 1823 | GFP_KERNEL); |
| 1824 | if (!tinfo->buf) |
| 1825 | goto outtinfo; |
Dan Carpenter | b425df4 | 2013-01-25 18:44:57 +0300 | [diff] [blame] | 1826 | } else { |
| 1827 | tinfo->buf = NULL; |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1828 | } |
| 1829 | tinfo->id = id; |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1830 | |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1831 | task = kthread_run(threadfn, tinfo, name, ipvs->gen, id); |
| 1832 | if (IS_ERR(task)) { |
| 1833 | result = PTR_ERR(task); |
| 1834 | goto outtinfo; |
| 1835 | } |
| 1836 | tinfo = NULL; |
| 1837 | if (state == IP_VS_STATE_MASTER) |
| 1838 | ipvs->ms[id].master_thread = task; |
| 1839 | else |
| 1840 | array[id] = task; |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | /* mark as active */ |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1844 | |
| 1845 | if (state == IP_VS_STATE_BACKUP) |
| 1846 | ipvs->backup_threads = array; |
| 1847 | spin_lock_bh(&ipvs->sync_buff_lock); |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1848 | ipvs->sync_state |= state; |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1849 | spin_unlock_bh(&ipvs->sync_buff_lock); |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1850 | |
| 1851 | /* increase the module use count */ |
| 1852 | ip_vs_use_count_inc(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | |
| 1854 | return 0; |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1855 | |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1856 | outsocket: |
Hans Schillstrom | 1ae132b | 2011-05-03 22:09:30 +0200 | [diff] [blame] | 1857 | sk_release_kernel(sock->sk); |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1858 | |
| 1859 | outtinfo: |
| 1860 | if (tinfo) { |
| 1861 | sk_release_kernel(tinfo->sock->sk); |
| 1862 | kfree(tinfo->buf); |
| 1863 | kfree(tinfo); |
| 1864 | } |
| 1865 | count = id; |
| 1866 | while (count-- > 0) { |
| 1867 | if (state == IP_VS_STATE_MASTER) |
| 1868 | kthread_stop(ipvs->ms[count].master_thread); |
| 1869 | else |
| 1870 | kthread_stop(array[count]); |
| 1871 | } |
| 1872 | kfree(array); |
| 1873 | |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1874 | out: |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1875 | if (!(ipvs->sync_state & IP_VS_STATE_MASTER)) { |
| 1876 | kfree(ipvs->ms); |
| 1877 | ipvs->ms = NULL; |
| 1878 | } |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1879 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1883 | int stop_sync_thread(struct net *net, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | { |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1885 | struct netns_ipvs *ipvs = net_ipvs(net); |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1886 | struct task_struct **array; |
| 1887 | int id; |
Hans Schillstrom | 1ae132b | 2011-05-03 22:09:30 +0200 | [diff] [blame] | 1888 | int retc = -EINVAL; |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1889 | |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1890 | IP_VS_DBG(7, "%s(): pid %d\n", __func__, task_pid_nr(current)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1892 | if (state == IP_VS_STATE_MASTER) { |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1893 | if (!ipvs->ms) |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1894 | return -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1896 | /* |
| 1897 | * The lock synchronizes with sb_queue_tail(), so that we don't |
| 1898 | * add sync buffers to the queue, when we are already in |
| 1899 | * progress of stopping the master sync daemon. |
| 1900 | */ |
| 1901 | |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1902 | spin_lock_bh(&ipvs->sync_buff_lock); |
| 1903 | spin_lock(&ipvs->sync_lock); |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1904 | ipvs->sync_state &= ~IP_VS_STATE_MASTER; |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1905 | spin_unlock(&ipvs->sync_lock); |
| 1906 | spin_unlock_bh(&ipvs->sync_buff_lock); |
| 1907 | |
| 1908 | retc = 0; |
| 1909 | for (id = ipvs->threads_mask; id >= 0; id--) { |
| 1910 | struct ipvs_master_sync_state *ms = &ipvs->ms[id]; |
| 1911 | int ret; |
| 1912 | |
| 1913 | pr_info("stopping master sync thread %d ...\n", |
| 1914 | task_pid_nr(ms->master_thread)); |
| 1915 | cancel_delayed_work_sync(&ms->master_wakeup_work); |
| 1916 | ret = kthread_stop(ms->master_thread); |
| 1917 | if (retc >= 0) |
| 1918 | retc = ret; |
| 1919 | } |
| 1920 | kfree(ipvs->ms); |
| 1921 | ipvs->ms = NULL; |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1922 | } else if (state == IP_VS_STATE_BACKUP) { |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1923 | if (!ipvs->backup_threads) |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1924 | return -ESRCH; |
| 1925 | |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1926 | ipvs->sync_state &= ~IP_VS_STATE_BACKUP; |
Pablo Neira Ayuso | f73181c | 2012-05-08 19:40:30 +0200 | [diff] [blame] | 1927 | array = ipvs->backup_threads; |
| 1928 | retc = 0; |
| 1929 | for (id = ipvs->threads_mask; id >= 0; id--) { |
| 1930 | int ret; |
| 1931 | |
| 1932 | pr_info("stopping backup sync thread %d ...\n", |
| 1933 | task_pid_nr(array[id])); |
| 1934 | ret = kthread_stop(array[id]); |
| 1935 | if (retc >= 0) |
| 1936 | retc = ret; |
| 1937 | } |
| 1938 | kfree(array); |
| 1939 | ipvs->backup_threads = NULL; |
Sven Wegener | 998e7a7 | 2008-07-16 11:13:50 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | /* decrease the module use count */ |
| 1943 | ip_vs_use_count_dec(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | |
Hans Schillstrom | 1ae132b | 2011-05-03 22:09:30 +0200 | [diff] [blame] | 1945 | return retc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | } |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 1947 | |
| 1948 | /* |
| 1949 | * Initialize data struct for each netns |
| 1950 | */ |
Hans Schillstrom | 503cf15 | 2011-05-01 18:50:16 +0200 | [diff] [blame] | 1951 | int __net_init ip_vs_sync_net_init(struct net *net) |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 1952 | { |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1953 | struct netns_ipvs *ipvs = net_ipvs(net); |
| 1954 | |
Hans Schillstrom | ae1d48b | 2011-10-11 10:54:35 +0900 | [diff] [blame] | 1955 | __mutex_init(&ipvs->sync_mutex, "ipvs->sync_mutex", &__ipvs_sync_key); |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1956 | spin_lock_init(&ipvs->sync_lock); |
| 1957 | spin_lock_init(&ipvs->sync_buff_lock); |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 1958 | return 0; |
| 1959 | } |
| 1960 | |
Hans Schillstrom | 503cf15 | 2011-05-01 18:50:16 +0200 | [diff] [blame] | 1961 | void ip_vs_sync_net_cleanup(struct net *net) |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 1962 | { |
Hans Schillstrom | 1ae132b | 2011-05-03 22:09:30 +0200 | [diff] [blame] | 1963 | int retc; |
Hans Schillstrom | ae1d48b | 2011-10-11 10:54:35 +0900 | [diff] [blame] | 1964 | struct netns_ipvs *ipvs = net_ipvs(net); |
Hans Schillstrom | 1ae132b | 2011-05-03 22:09:30 +0200 | [diff] [blame] | 1965 | |
Hans Schillstrom | ae1d48b | 2011-10-11 10:54:35 +0900 | [diff] [blame] | 1966 | mutex_lock(&ipvs->sync_mutex); |
Hans Schillstrom | 1ae132b | 2011-05-03 22:09:30 +0200 | [diff] [blame] | 1967 | retc = stop_sync_thread(net, IP_VS_STATE_MASTER); |
| 1968 | if (retc && retc != -ESRCH) |
| 1969 | pr_err("Failed to stop Master Daemon\n"); |
| 1970 | |
| 1971 | retc = stop_sync_thread(net, IP_VS_STATE_BACKUP); |
| 1972 | if (retc && retc != -ESRCH) |
| 1973 | pr_err("Failed to stop Backup Daemon\n"); |
Hans Schillstrom | ae1d48b | 2011-10-11 10:54:35 +0900 | [diff] [blame] | 1974 | mutex_unlock(&ipvs->sync_mutex); |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 1975 | } |