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