blob: b4f6b979249dbd2905495661cfb8a9e596933c62 [file] [log] [blame]
Emeric Brun2b920a12010-09-23 18:30:22 +02001/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02002 * Peer synchro management.
Emeric Brun2b920a12010-09-23 18:30:22 +02003 *
4 * Copyright 2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <common/compat.h>
24#include <common/config.h>
25#include <common/time.h>
26
27#include <types/global.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010028#include <types/listener.h>
29#include <types/obj_type.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020030#include <types/peers.h>
31
32#include <proto/acl.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020033#include <proto/applet.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020034#include <proto/channel.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020035#include <proto/fd.h>
Willy Tarreaud1d48d42015-03-13 16:15:46 +010036#include <proto/frontend.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020037#include <proto/log.h>
38#include <proto/hdr_idx.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020039#include <proto/proto_tcp.h>
40#include <proto/proto_http.h>
41#include <proto/proxy.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020042#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020043#include <proto/stream.h>
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010044#include <proto/signal.h>
45#include <proto/stick_table.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020046#include <proto/stream_interface.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020047#include <proto/task.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020048
49
50/*******************************/
51/* Current peer learning state */
52/*******************************/
53
54/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020055/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020056/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020057#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
58#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
59#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
60#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
61#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
Emeric Brun2b920a12010-09-23 18:30:22 +020062 to push data to new process */
63
Emeric Brunb3971ab2015-05-12 18:49:09 +020064#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
65#define PEERS_RESYNC_FROMLOCAL 0x00000000
66#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
67#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
68
69/***********************************/
70/* Current shared table sync state */
71/***********************************/
72#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
73#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020074
75/******************************/
76/* Remote peer teaching state */
77/******************************/
78#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
Emeric Brun2b920a12010-09-23 18:30:22 +020079#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
80#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
81#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
82#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
83
Emeric Brunb3971ab2015-05-12 18:49:09 +020084#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
Emeric Brun2b920a12010-09-23 18:30:22 +020085#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
86
Emeric Brunb3971ab2015-05-12 18:49:09 +020087/*****************************/
88/* Sync message class */
89/*****************************/
90enum {
91 PEER_MSG_CLASS_CONTROL = 0,
92 PEER_MSG_CLASS_ERROR,
93 PEER_MSG_CLASS_STICKTABLE = 10,
94 PEER_MSG_CLASS_RESERVED = 255,
95};
96
97/*****************************/
98/* control message types */
99/*****************************/
100enum {
101 PEER_MSG_CTRL_RESYNCREQ = 0,
102 PEER_MSG_CTRL_RESYNCFINISHED,
103 PEER_MSG_CTRL_RESYNCPARTIAL,
104 PEER_MSG_CTRL_RESYNCCONFIRM,
105};
106
107/*****************************/
108/* error message types */
109/*****************************/
110enum {
111 PEER_MSG_ERR_PROTOCOL = 0,
112 PEER_MSG_ERR_SIZELIMIT,
113};
114
115
116/*******************************/
117/* stick table sync mesg types */
118/* Note: ids >= 128 contains */
119/* id message cotains data */
120/*******************************/
121enum {
122 PEER_MSG_STKT_UPDATE = 128,
123 PEER_MSG_STKT_INCUPDATE,
124 PEER_MSG_STKT_DEFINE,
125 PEER_MSG_STKT_SWITCH,
126 PEER_MSG_STKT_ACK,
127};
Emeric Brun2b920a12010-09-23 18:30:22 +0200128
129/**********************************/
130/* Peer Session IO handler states */
131/**********************************/
132
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100133enum {
134 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
135 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
136 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
137 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100138 /* after this point, data were possibly exchanged */
139 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
140 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
141 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
142 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
143 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200144 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
145 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100146 PEER_SESS_ST_END, /* Killed session */
147};
Emeric Brun2b920a12010-09-23 18:30:22 +0200148
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100149/***************************************************/
150/* Peer Session status code - part of the protocol */
151/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200152
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100153#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
154#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200155
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100156#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200157
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100158#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200159
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100160#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
161#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
162#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
163#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200164
165#define PEER_SESSION_PROTO_NAME "HAProxyS"
166
167struct peers *peers = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +0200168static void peer_session_forceshutdown(struct stream * stream);
Emeric Brun2b920a12010-09-23 18:30:22 +0200169
Emeric Brunb3971ab2015-05-12 18:49:09 +0200170int intencode(uint64_t i, char **str) {
171 int idx = 0;
172 unsigned char *msg;
173
174 if (!*str)
175 return 0;
176
177 msg = (unsigned char *)*str;
178 if (i < 240) {
179 msg[0] = (unsigned char)i;
180 *str = (char *)&msg[idx+1];
181 return (idx+1);
182 }
183
184 msg[idx] =(unsigned char)i | 240;
185 i = (i - 240) >> 4;
186 while (i >= 128) {
187 msg[++idx] = (unsigned char)i | 128;
188 i = (i - 128) >> 7;
189 }
190 msg[++idx] = (unsigned char)i;
191 *str = (char *)&msg[idx+1];
192 return (idx+1);
193}
194
195
196/* This function returns the decoded integer or 0
197 if decode failed
198 *str point on the beginning of the integer to decode
199 at the end of decoding *str point on the end of the
200 encoded integer or to null if end is reached */
201uint64_t intdecode(char **str, char *end) {
202 uint64_t i;
203 int idx = 0;
204 unsigned char *msg;
205
206 if (!*str)
207 return 0;
208
209 msg = (unsigned char *)*str;
210 if (msg >= (unsigned char *)end) {
211 *str = NULL;
212 return 0;
213 }
214
215 if (msg[idx] < 240) {
216 *str = (char *)&msg[idx+1];
217 return msg[idx];
218 }
219 i = msg[idx];
220 do {
221 idx++;
222 if (msg >= (unsigned char *)end) {
223 *str = NULL;
224 return 0;
225 }
226 i += (uint64_t)msg[idx] << (4 + 7*(idx-1));
227 }
228 while (msg[idx] > 128);
229 *str = (char *)&msg[idx+1];
230 return i;
231}
Emeric Brun2b920a12010-09-23 18:30:22 +0200232
233/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200234 * This prepare the data update message on the stick session <ts>, <st> is the considered
235 * stick table.
236 * <msg> is a buffer of <size> to recieve data message content
237 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
238 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200239 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200240static int peer_prepare_updatemsg(struct stksess *ts, struct shared_table *st, char *msg, size_t size, int use_identifier)
Emeric Brun2b920a12010-09-23 18:30:22 +0200241{
242 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200243 unsigned short datalen;
244 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200245 unsigned int data_type;
246 void *data_ptr;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200247
248 cursor = datamsg = msg + 1 + 5;
249
Emeric Brun2b920a12010-09-23 18:30:22 +0200250 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200251
252 /* check if we need to send the update identifer */
253 if (st->last_pushed && ts->upd.key > st->last_pushed && (ts->upd.key - st->last_pushed) == 1) {
254 use_identifier = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200255 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200256
257 /* encode update identifier if needed */
258 if (use_identifier) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200259 netinteger = htonl(ts->upd.key);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200260 memcpy(cursor, &netinteger, sizeof(netinteger));
261 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200262 }
263
Emeric Brunb3971ab2015-05-12 18:49:09 +0200264 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200265 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200266 int stlen = strlen((char *)ts->key.key);
267
Emeric Brunb3971ab2015-05-12 18:49:09 +0200268 intencode(stlen, &cursor);
269 memcpy(cursor, ts->key.key, stlen);
270 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200271 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200272 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200273 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200274 memcpy(cursor, &netinteger, sizeof(netinteger));
275 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200276 }
277 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200278 memcpy(cursor, ts->key.key, st->table->key_size);
279 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200280 }
281
Emeric Brunb3971ab2015-05-12 18:49:09 +0200282 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200283 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200284
Emeric Brun94900952015-06-11 18:25:54 +0200285 data_ptr = stktable_data_ptr(st->table, ts, data_type);
286 if (data_ptr) {
287 switch (stktable_data_types[data_type].std_type) {
288 case STD_T_SINT: {
289 int data;
290
291 data = stktable_data_cast(data_ptr, std_t_sint);
292 intencode(data, &cursor);
293 break;
294 }
295 case STD_T_UINT: {
296 unsigned int data;
297
298 data = stktable_data_cast(data_ptr, std_t_uint);
299 intencode(data, &cursor);
300 break;
301 }
302 case STD_T_ULL: {
303 unsigned long long data;
304
305 data = stktable_data_cast(data_ptr, std_t_ull);
306 intencode(data, &cursor);
307 break;
308 }
309 case STD_T_FRQP: {
310 struct freq_ctr_period *frqp;
311
312 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
313 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
314 intencode(frqp->curr_ctr, &cursor);
315 intencode(frqp->prev_ctr, &cursor);
316 break;
317 }
318 }
319 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200320 }
321
322 /* Compute datalen */
323 datalen = (cursor - datamsg);
324
325 /* prepare message header */
326 msg[0] = PEER_MSG_CLASS_STICKTABLE;
327 if (use_identifier)
328 msg[1] = PEER_MSG_STKT_UPDATE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200329 else
Emeric Brunb3971ab2015-05-12 18:49:09 +0200330 msg[1] = PEER_MSG_STKT_INCUPDATE;
331
332 cursor = &msg[2];
333 intencode(datalen, &cursor);
334
335 /* move data after header */
336 memmove(cursor, datamsg, datalen);
337
338 /* return header size + data_len */
339 return (cursor - msg) + datalen;
340}
341
342/*
343 * This prepare the switch table message to targeted share table <st>.
344 * <msg> is a buffer of <size> to recieve data message content
345 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
346 * check size)
347 */
348static int peer_prepare_switchmsg(struct shared_table *st, char *msg, size_t size)
349{
350 int len;
351 unsigned short datalen;
352 char *cursor, *datamsg;
353 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200354 unsigned int data_type;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200355
356 cursor = datamsg = msg + 2 + 5;
357
358 /* Encode data */
359
360 /* encode local id */
361 intencode(st->local_id, &cursor);
362
363 /* encode table name */
364 len = strlen(st->table->id);
365 intencode(len, &cursor);
366 memcpy(cursor, st->table->id, len);
367 cursor += len;
368
369 /* encode table type */
370
371 intencode(st->table->type, &cursor);
372
373 /* encode table key size */
374 intencode(st->table->key_size, &cursor);
375
Emeric Brun94900952015-06-11 18:25:54 +0200376 /* encode available known data types in table */
377 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
378 if (st->table->data_ofs[data_type]) {
379 switch (stktable_data_types[data_type].std_type) {
380 case STD_T_SINT:
381 case STD_T_UINT:
382 case STD_T_ULL:
383 case STD_T_FRQP:
384 data |= 1 << data_type;
385 break;
386 }
387 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200388 }
389 intencode(data, &cursor);
390
391 /* Compute datalen */
392 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200393
Emeric Brunb3971ab2015-05-12 18:49:09 +0200394 /* prepare message header */
395 msg[0] = PEER_MSG_CLASS_STICKTABLE;
396 msg[1] = PEER_MSG_STKT_DEFINE;
397 cursor = &msg[2];
398 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200399
Emeric Brunb3971ab2015-05-12 18:49:09 +0200400 /* move data after header */
401 memmove(cursor, datamsg, datalen);
402
403 /* return header size + data_len */
404 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200405}
406
Emeric Brunb3971ab2015-05-12 18:49:09 +0200407/*
408 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
409 * stick table.
410 * <msg> is a buffer of <size> to recieve data message content
411 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
412 * check size)
413 */
414static int peer_prepare_ackmsg(struct shared_table *st, char *msg, size_t size)
415{
416 unsigned short datalen;
417 char *cursor, *datamsg;
418 uint32_t netinteger;
419
420 cursor = datamsg = trash.str + 2 + 5;
421
422 intencode(st->remote_id, &cursor);
423 netinteger = htonl(st->last_get);
424 memcpy(cursor, &netinteger, sizeof(netinteger));
425 cursor += sizeof(netinteger);
426
427 /* Compute datalen */
428 datalen = (cursor - datamsg);
429
430 /* prepare message header */
431 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200432 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200433 cursor = &msg[2];
434 intencode(datalen, &cursor);
435
436 /* move data after header */
437 memmove(cursor, datamsg, datalen);
438
439 /* return header size + data_len */
440 return (cursor - msg) + datalen;
441}
Emeric Brun2b920a12010-09-23 18:30:22 +0200442
443/*
444 * Callback to release a session with a peer
445 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200446static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200447{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200448 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200449 struct stream *s = si_strm(si);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200450 struct peer *peer = (struct peer *)appctx->ctx.peers.ptr;
451 struct peers *peers = (struct peers *)strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200452
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100453 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100454 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200455 return;
456
457 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200458 if (peer) {
459 if (peer->stream == s) {
460 peer->stream = NULL;
461 peer->appctx = NULL;
462 if (peer->flags & PEER_F_LEARN_ASSIGN) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200463 /* unassign current peer for learning */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200464 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
465 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brun2b920a12010-09-23 18:30:22 +0200466
467 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200468 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +0200469 }
470 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200471 peer->flags &= PEER_TEACH_RESET;
472 peer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200473 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200474 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200475 }
476}
477
478
479/*
480 * IO Handler to handle message exchance with a peer
481 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200482static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200483{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200484 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200485 struct stream *s = si_strm(si);
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200486 struct peers *curpeers = (struct peers *)strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200487 int reql = 0;
488 int repl = 0;
489
490 while (1) {
491switchstate:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100492 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100493 case PEER_SESS_ST_ACCEPT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100494 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100495 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200496 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100497 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100498 reql = bo_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200499 if (reql <= 0) { /* closed or EOL not found */
500 if (reql == 0)
501 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100502 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200503 goto switchstate;
504 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100505 if (trash.str[reql-1] != '\n') {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100506 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200507 goto switchstate;
508 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100509 else if (reql > 1 && (trash.str[reql-2] == '\r'))
510 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200511 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100512 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200513
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100514 bo_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200515
516 /* test version */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200517 if (strcmp(PEER_SESSION_PROTO_NAME " 2.0", trash.str) != 0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100518 appctx->st0 = PEER_SESS_ST_EXIT;
519 appctx->st1 = PEER_SESS_SC_ERRVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200520 /* test protocol */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100521 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.str, strlen(PEER_SESSION_PROTO_NAME)+1) != 0)
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100522 appctx->st1 = PEER_SESS_SC_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +0200523 goto switchstate;
524 }
525
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100526 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200527 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100528 case PEER_SESS_ST_GETHOST:
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100529 reql = bo_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200530 if (reql <= 0) { /* closed or EOL not found */
531 if (reql == 0)
532 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100533 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200534 goto switchstate;
535 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100536 if (trash.str[reql-1] != '\n') {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100537 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200538 goto switchstate;
539 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100540 else if (reql > 1 && (trash.str[reql-2] == '\r'))
541 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200542 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100543 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200544
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100545 bo_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200546
547 /* test hostname match */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100548 if (strcmp(localpeer, trash.str) != 0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100549 appctx->st0 = PEER_SESS_ST_EXIT;
550 appctx->st1 = PEER_SESS_SC_ERRHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200551 goto switchstate;
552 }
553
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100554 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200555 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100556 case PEER_SESS_ST_GETPEER: {
Emeric Brun2b920a12010-09-23 18:30:22 +0200557 struct peer *curpeer;
558 char *p;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100559 reql = bo_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200560 if (reql <= 0) { /* closed or EOL not found */
561 if (reql == 0)
562 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100563 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200564 goto switchstate;
565 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100566 if (trash.str[reql-1] != '\n') {
Emeric Brun2b920a12010-09-23 18:30:22 +0200567 /* Incomplete line, we quit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100568 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200569 goto switchstate;
570 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100571 else if (reql > 1 && (trash.str[reql-2] == '\r'))
572 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200573 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100574 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200575
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100576 bo_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200577
Emeric Brunb3971ab2015-05-12 18:49:09 +0200578 /* parse line "<peer name> <pid> <relative_pid>" */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100579 p = strchr(trash.str, ' ');
Emeric Brun2b920a12010-09-23 18:30:22 +0200580 if (!p) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100581 appctx->st0 = PEER_SESS_ST_EXIT;
582 appctx->st1 = PEER_SESS_SC_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +0200583 goto switchstate;
584 }
585 *p = 0;
586
587 /* lookup known peer */
588 for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100589 if (strcmp(curpeer->id, trash.str) == 0)
Emeric Brun2b920a12010-09-23 18:30:22 +0200590 break;
591 }
592
593 /* if unknown peer */
594 if (!curpeer) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100595 appctx->st0 = PEER_SESS_ST_EXIT;
596 appctx->st1 = PEER_SESS_SC_ERRPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200597 goto switchstate;
598 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200599
Emeric Brunb3971ab2015-05-12 18:49:09 +0200600 if (curpeer->stream && curpeer->stream != s) {
601 if (curpeer->local) {
602 /* Local connection, reply a retry */
603 appctx->st0 = PEER_SESS_ST_EXIT;
604 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
605 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200606 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200607 peer_session_forceshutdown(curpeer->stream);
Emeric Brun2b920a12010-09-23 18:30:22 +0200608 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200609 curpeer->stream = s;
610 curpeer->appctx = appctx;
611 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100612 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200613 /* fall through */
614 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100615 case PEER_SESS_ST_SENDSUCCESS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200616 struct peer *curpeer = (struct peer *)appctx->ctx.peers.ptr;
617 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200618
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100619 repl = snprintf(trash.str, trash.size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100620 repl = bi_putblk(si_ic(si), trash.str, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200621 if (repl <= 0) {
622 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100623 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100624 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200625 goto switchstate;
626 }
627
628 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200629 curpeer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200630
631 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200632 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200633
634 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200635 curpeer->confirm = 0;
636
637 /* Init cursors */
638 for (st = curpeer->tables; st ; st = st->next) {
639 st->last_get = st->last_acked = 0;
640 st->teaching_origin = st->last_pushed = st->update;
641 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200642
643 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200644 curpeer->flags &= PEER_TEACH_RESET;
645 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200646
647 /* if current peer is local */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200648 if (curpeer->local) {
649 /* if current host need resyncfrom local and no process assined */
650 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
651 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
652 /* assign local peer for a lesson, consider lesson already requested */
653 curpeer->flags |= PEER_F_LEARN_ASSIGN;
654 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
655 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200656
Emeric Brunb3971ab2015-05-12 18:49:09 +0200657 }
658 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
659 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
660 /* assign peer for a lesson */
661 curpeer->flags |= PEER_F_LEARN_ASSIGN;
662 peers->flags |= PEERS_F_RESYNC_ASSIGN;
663 }
664
665
Emeric Brun2b920a12010-09-23 18:30:22 +0200666 /* switch to waiting message state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100667 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200668 goto switchstate;
669 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100670 case PEER_SESS_ST_CONNECT: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200671 struct peer *curpeer = (struct peer *)appctx->ctx.peers.ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +0200672
673 /* Send headers */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100674 repl = snprintf(trash.str, trash.size,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200675 PEER_SESSION_PROTO_NAME " 2.0\n%s\n%s %d %d\n",
676 curpeer->id,
Emeric Brun2b920a12010-09-23 18:30:22 +0200677 localpeer,
Willy Tarreau7b77c9f2012-01-07 22:52:12 +0100678 (int)getpid(),
Emeric Brunb3971ab2015-05-12 18:49:09 +0200679 relative_pid);
Emeric Brun2b920a12010-09-23 18:30:22 +0200680
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100681 if (repl >= trash.size) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100682 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200683 goto switchstate;
684 }
685
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100686 repl = bi_putblk(si_ic(si), trash.str, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200687 if (repl <= 0) {
688 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100689 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100690 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200691 goto switchstate;
692 }
693
694 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100695 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200696 /* fall through */
697 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100698 case PEER_SESS_ST_GETSTATUS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200699 struct peer *curpeer = (struct peer *)appctx->ctx.peers.ptr;
700 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200701
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100702 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200703 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200704
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100705 reql = bo_getline(si_oc(si), trash.str, trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200706 if (reql <= 0) { /* closed or EOL not found */
707 if (reql == 0)
708 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100709 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200710 goto switchstate;
711 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100712 if (trash.str[reql-1] != '\n') {
Emeric Brun2b920a12010-09-23 18:30:22 +0200713 /* Incomplete line, we quit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100714 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200715 goto switchstate;
716 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100717 else if (reql > 1 && (trash.str[reql-2] == '\r'))
718 trash.str[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200719 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100720 trash.str[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200721
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100722 bo_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200723
724 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200725 curpeer->statuscode = atoi(trash.str);
Emeric Brun2b920a12010-09-23 18:30:22 +0200726
727 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200728 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200729
730 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200731 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200732 /* Init cursors */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200733 for (st = curpeer->tables; st ; st = st->next) {
734 st->last_get = st->last_acked = 0;
735 st->teaching_origin = st->last_pushed = st->update;
736 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200737
738 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200739 curpeer->confirm = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200740
Emeric Brunb3971ab2015-05-12 18:49:09 +0200741 /* reset teaching and learning flags to 0 */
742 curpeer->flags &= PEER_TEACH_RESET;
743 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200744
Emeric Brunb3971ab2015-05-12 18:49:09 +0200745 /* If current peer is local */
746 if (curpeer->local) {
747 /* flag to start to teach lesson */
748 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200749
Emeric Brunb3971ab2015-05-12 18:49:09 +0200750 }
751 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
752 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
753 /* If peer is remote and resync from remote is needed,
754 and no peer currently assigned */
Emeric Brun2b920a12010-09-23 18:30:22 +0200755
Emeric Brunb3971ab2015-05-12 18:49:09 +0200756 /* assign peer for a lesson */
757 curpeer->flags |= PEER_F_LEARN_ASSIGN;
758 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +0200759 }
760
761 }
762 else {
763 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100764 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200765 goto switchstate;
766 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100767 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200768 /* fall through */
769 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100770 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200771 struct peer *curpeer = (struct peer *)appctx->ctx.peers.ptr;
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200772 struct stksess *ts, *newts = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200773 uint32_t msg_len = 0;
774 char *msg_cur = trash.str;
775 char *msg_end = trash.str;
776 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +0200777 int totl = 0;
778
Emeric Brunb3971ab2015-05-12 18:49:09 +0200779 reql = bo_getblk(si_oc(si), (char *)msg_head, 2*sizeof(unsigned char), totl);
Willy Tarreau72d6c162013-04-11 16:14:13 +0200780 if (reql <= 0) /* closed or EOL not found */
781 goto incomplete;
782
Emeric Brun2b920a12010-09-23 18:30:22 +0200783 totl += reql;
784
Emeric Brunb3971ab2015-05-12 18:49:09 +0200785 if (msg_head[1] >= 128) {
786 /* Read and Decode message length */
787 reql = bo_getblk(si_oc(si), (char *)&msg_head[2], sizeof(unsigned char), totl);
788 if (reql <= 0) /* closed */
789 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200790
Emeric Brunb3971ab2015-05-12 18:49:09 +0200791 totl += reql;
792
793 if (msg_head[2] < 240) {
794 msg_len = msg_head[2];
Emeric Brun2b920a12010-09-23 18:30:22 +0200795 }
796 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200797 int i;
798 char *cur;
799 char *end;
Willy Tarreau72d6c162013-04-11 16:14:13 +0200800
Emeric Brunb3971ab2015-05-12 18:49:09 +0200801 for (i = 3 ; i < sizeof(msg_head) ; i++) {
802 reql = bo_getblk(si_oc(si), (char *)&msg_head[i], sizeof(char), totl);
803 if (reql <= 0) /* closed */
804 goto incomplete;
805
806 totl += reql;
807
808 if (!(msg_head[i] & 0x80))
809 break;
810 }
811
812 if (i == sizeof(msg_head)) {
813 /* malformed message */
814 appctx->st0 = PEER_SESS_ST_ERRPROTO;
815 goto switchstate;
816
817 }
818 end = (char *)msg_head + sizeof(msg_head);
819 cur = (char *)&msg_head[2];
820 msg_len = intdecode(&cur, end);
821 if (!cur) {
822 /* malformed message */
823 appctx->st0 = PEER_SESS_ST_ERRPROTO;
824 goto switchstate;
825 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200826 }
827
Willy Tarreau86a446e2013-11-25 23:02:37 +0100828
Emeric Brunb3971ab2015-05-12 18:49:09 +0200829 /* Read message content */
830 if (msg_len) {
831 if (msg_len > trash.size) {
832 /* Status code is not success, abort */
833 appctx->st0 = PEER_SESS_ST_ERRSIZE;
834 goto switchstate;
835 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200836
Emeric Brunb3971ab2015-05-12 18:49:09 +0200837 reql = bo_getblk(si_oc(si), trash.str, msg_len, totl);
838 if (reql <= 0) /* closed */
839 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200840 totl += reql;
Willy Tarreau86a446e2013-11-25 23:02:37 +0100841
Emeric Brunb3971ab2015-05-12 18:49:09 +0200842 msg_end += msg_len;
843 }
844 }
Willy Tarreau86a446e2013-11-25 23:02:37 +0100845
Emeric Brunb3971ab2015-05-12 18:49:09 +0200846 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
847 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
848 struct shared_table *st;
849 /* Reset message: remote need resync */
850
851 /* prepare tables fot a global push */
852 for (st = curpeer->tables; st; st = st->next) {
853 st->teaching_origin = st->last_pushed = st->table->update;
854 st->flags = 0;
Willy Tarreau86a446e2013-11-25 23:02:37 +0100855 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200856
Emeric Brunb3971ab2015-05-12 18:49:09 +0200857 /* reset teaching flags to 0 */
858 curpeer->flags &= PEER_TEACH_RESET;
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200859
Emeric Brunb3971ab2015-05-12 18:49:09 +0200860 /* flag to start to teach lesson */
861 curpeer->flags |= PEER_F_TEACH_PROCESS;
862
863
864 }
865 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
866
867 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
868 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
869 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
870 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Willy Tarreau86a446e2013-11-25 23:02:37 +0100871 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200872 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +0200873 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200874 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
875
876 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
877 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
878 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
879
880 curpeer->flags |= PEER_F_LEARN_NOTUP2DATE;
881 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
882 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Cyril Bonté9a60ff92014-02-16 01:07:07 +0100883 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200884 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +0200885 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200886 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
887
888 /* If stopping state */
889 if (stopping) {
890 /* Close session, push resync no more needed */
891 curpeer->flags |= PEER_F_TEACH_COMPLETE;
892 appctx->st0 = PEER_SESS_ST_END;
893 goto switchstate;
894 }
895
896 /* reset teaching flags to 0 */
897 curpeer->flags &= PEER_TEACH_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200898 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200899 }
900 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
901 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
902 int table_id_len;
903 struct shared_table *st;
904 int table_type;
905 int table_keylen;
906 int table_id;
907 uint64_t table_data;
Emeric Brun2b920a12010-09-23 18:30:22 +0200908
Emeric Brunb3971ab2015-05-12 18:49:09 +0200909 table_id = intdecode(&msg_cur, msg_end);
910 if (!msg_cur) {
911 /* malformed message */
912 appctx->st0 = PEER_SESS_ST_ERRPROTO;
913 goto switchstate;
914 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200915
Emeric Brunb3971ab2015-05-12 18:49:09 +0200916 table_id_len = intdecode(&msg_cur, msg_end);
917 if (!msg_cur) {
918 /* malformed message */
919 appctx->st0 = PEER_SESS_ST_ERRPROTO;
920 goto switchstate;
921 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200922
Emeric Brunb3971ab2015-05-12 18:49:09 +0200923 curpeer->remote_table = NULL;
924 if (!table_id_len || (msg_cur + table_id_len) >= msg_end) {
925 /* malformed message */
926 appctx->st0 = PEER_SESS_ST_ERRPROTO;
927 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200928 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200929
Emeric Brunb3971ab2015-05-12 18:49:09 +0200930 for (st = curpeer->tables; st; st = st->next) {
931 /* Reset IDs */
932 if (st->remote_id == table_id)
933 st->remote_id = 0;
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200934
Emeric Brunb3971ab2015-05-12 18:49:09 +0200935 if (!curpeer->remote_table
936 && (table_id_len == strlen(st->table->id))
937 && (memcmp(st->table->id, msg_cur, table_id_len) == 0)) {
938 curpeer->remote_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200939 }
940 }
941
Emeric Brunb3971ab2015-05-12 18:49:09 +0200942 if (!curpeer->remote_table) {
943 goto ignore_msg;
944 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200945
Emeric Brunb3971ab2015-05-12 18:49:09 +0200946 msg_cur += table_id_len;
947 if (msg_cur >= msg_end) {
948 /* malformed message */
949 appctx->st0 = PEER_SESS_ST_ERRPROTO;
950 goto switchstate;
951 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200952
Emeric Brunb3971ab2015-05-12 18:49:09 +0200953 table_type = intdecode(&msg_cur, msg_end);
954 if (!msg_cur) {
955 /* malformed message */
956 appctx->st0 = PEER_SESS_ST_ERRPROTO;
957 goto switchstate;
958 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200959
Emeric Brunb3971ab2015-05-12 18:49:09 +0200960 table_keylen = intdecode(&msg_cur, msg_end);
961 if (!msg_cur) {
962 /* malformed message */
963 appctx->st0 = PEER_SESS_ST_ERRPROTO;
964 goto switchstate;
965 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200966
Emeric Brunb3971ab2015-05-12 18:49:09 +0200967 table_data = intdecode(&msg_cur, msg_end);
968 if (!msg_cur) {
969 /* malformed message */
970 appctx->st0 = PEER_SESS_ST_ERRPROTO;
971 goto switchstate;
972 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200973
Emeric Brunb3971ab2015-05-12 18:49:09 +0200974 if (curpeer->remote_table->table->type != table_type
975 || curpeer->remote_table->table->key_size != table_keylen) {
976 curpeer->remote_table = NULL;
977 goto ignore_msg;
978 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200979
Emeric Brunb3971ab2015-05-12 18:49:09 +0200980 curpeer->remote_table->remote_data = table_data;
981 curpeer->remote_table->remote_id = table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +0200982 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200983 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
984 struct shared_table *st;
985 int table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +0200986
Emeric Brunb3971ab2015-05-12 18:49:09 +0200987 table_id = intdecode(&msg_cur, msg_end);
988 if (!msg_cur) {
989 /* malformed message */
990 appctx->st0 = PEER_SESS_ST_ERRPROTO;
991 goto switchstate;
992 }
993 curpeer->remote_table = NULL;
994 for (st = curpeer->tables; st; st = st->next) {
995 if (st->remote_id == table_id) {
996 curpeer->remote_table = st;
997 break;
998 }
999 }
1000
Emeric Brun2b920a12010-09-23 18:30:22 +02001001 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001002 else if (msg_head[1] == PEER_MSG_STKT_UPDATE
1003 || msg_head[1] == PEER_MSG_STKT_INCUPDATE) {
1004 struct shared_table *st = curpeer->remote_table;
1005 uint32_t update;
Emeric Brun94900952015-06-11 18:25:54 +02001006 unsigned int data_type;
1007 void *data_ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +02001008
Emeric Brunb3971ab2015-05-12 18:49:09 +02001009 /* Here we have data message */
1010 if (!st)
1011 goto ignore_msg;
Emeric Brun2b920a12010-09-23 18:30:22 +02001012
Emeric Brunb3971ab2015-05-12 18:49:09 +02001013 if (msg_head[1] == PEER_MSG_STKT_UPDATE) {
1014 if (msg_len < sizeof(update)) {
1015 /* malformed message */
1016 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1017 goto switchstate;
1018 }
1019 memcpy(&update, msg_cur, sizeof(update));
1020 msg_cur += sizeof(update);
1021 st->last_get = htonl(update);
1022 }
1023 else {
1024 st->last_get++;
1025 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001026
Emeric Brunb3971ab2015-05-12 18:49:09 +02001027 newts = stksess_new(st->table, NULL);
1028 if (!newts)
1029 goto ignore_msg;
Emeric Brun2b920a12010-09-23 18:30:22 +02001030
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001031 if (st->table->type == SMP_T_STR) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001032 unsigned int to_read, to_store;
Emeric Brun2b920a12010-09-23 18:30:22 +02001033
Emeric Brunb3971ab2015-05-12 18:49:09 +02001034 to_read = intdecode(&msg_cur, msg_end);
1035 if (!msg_cur) {
1036 /* malformed message */
1037 stksess_free(st->table, newts);
1038 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1039 goto switchstate;
1040 }
1041 to_store = MIN(to_read, st->table->key_size - 1);
1042 if (msg_cur + to_store > msg_end) {
1043 /* malformed message */
1044 stksess_free(st->table, newts);
1045 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1046 goto switchstate;
1047 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001048
Emeric Brunb3971ab2015-05-12 18:49:09 +02001049 memcpy(newts->key.key, msg_cur, to_store);
1050 newts->key.key[to_store] = 0;
1051 msg_cur += to_read;
1052 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001053 else if (st->table->type == SMP_T_SINT) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001054 unsigned int netinteger;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001055
Emeric Brunb3971ab2015-05-12 18:49:09 +02001056 if (msg_cur + sizeof(netinteger) > msg_end) {
1057 /* malformed message */
1058 stksess_free(st->table, newts);
1059 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1060 goto switchstate;
1061 }
1062 memcpy(&netinteger, msg_cur, sizeof(netinteger));
1063 netinteger = ntohl(netinteger);
1064 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1065 msg_cur += sizeof(netinteger);
1066 }
1067 else {
1068 if (msg_cur + st->table->key_size > msg_end) {
1069 /* malformed message */
1070 stksess_free(st->table, newts);
1071 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1072 goto switchstate;
1073 }
1074 memcpy(newts->key.key, msg_cur, st->table->key_size);
1075 msg_cur += st->table->key_size;
1076 }
1077
1078 /* lookup for existing entry */
1079 ts = stktable_lookup(st->table, newts);
1080 if (ts) {
1081 /* the entry already exist, we can free ours */
1082 stktable_touch(st->table, ts, 0);
1083 stksess_free(st->table, newts);
1084 newts = NULL;
1085 }
1086 else {
1087 struct eb32_node *eb;
1088
1089 /* create new entry */
1090 ts = stktable_store(st->table, newts, 0);
1091 newts = NULL; /* don't reuse it */
1092
1093 ts->upd.key= (++st->table->update)+(2^31);
1094 eb = eb32_insert(&st->table->updates, &ts->upd);
1095 if (eb != &ts->upd) {
1096 eb32_delete(eb);
1097 eb32_insert(&st->table->updates, &ts->upd);
1098 }
1099 }
1100
Emeric Brun94900952015-06-11 18:25:54 +02001101 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001102
Emeric Brun94900952015-06-11 18:25:54 +02001103 if ((1 << data_type) & st->remote_data) {
1104 switch (stktable_data_types[data_type].std_type) {
1105 case STD_T_SINT: {
1106 int data;
1107
1108 data = intdecode(&msg_cur, msg_end);
1109 if (!msg_cur) {
1110 /* malformed message */
1111 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1112 goto switchstate;
1113 }
1114
1115 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1116 if (data_ptr)
1117 stktable_data_cast(data_ptr, std_t_sint) = data;
1118 break;
1119 }
1120 case STD_T_UINT: {
1121 unsigned int data;
1122
1123 data = intdecode(&msg_cur, msg_end);
1124 if (!msg_cur) {
1125 /* malformed message */
1126 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1127 goto switchstate;
1128 }
1129
1130 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1131 if (data_ptr)
1132 stktable_data_cast(data_ptr, std_t_uint) = data;
1133 break;
1134 }
1135 case STD_T_ULL: {
1136 unsigned long long data;
1137
1138 data = intdecode(&msg_cur, msg_end);
1139 if (!msg_cur) {
1140 /* malformed message */
1141 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1142 goto switchstate;
1143 }
1144
1145 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1146 if (data_ptr)
1147 stktable_data_cast(data_ptr, std_t_ull) = data;
1148 break;
1149 }
1150 case STD_T_FRQP: {
1151 struct freq_ctr_period data;
1152
1153 data.curr_tick = tick_add(now_ms, intdecode(&msg_cur, msg_end));
1154 if (!msg_cur) {
1155 /* malformed message */
1156 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1157 goto switchstate;
1158 }
1159 data.curr_ctr = intdecode(&msg_cur, msg_end);
1160 if (!msg_cur) {
1161 /* malformed message */
1162 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1163 goto switchstate;
1164 }
1165 data.prev_ctr = intdecode(&msg_cur, msg_end);
1166 if (!msg_cur) {
1167 /* malformed message */
1168 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1169 goto switchstate;
1170 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001171
Emeric Brun94900952015-06-11 18:25:54 +02001172 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1173 if (data_ptr)
1174 stktable_data_cast(data_ptr, std_t_frqp) = data;
1175 break;
1176 }
1177 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001178 }
1179 }
1180 }
1181 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1182 /* ack message */
1183 uint32_t table_id ;
1184 uint32_t update;
1185 struct shared_table *st;
1186
1187 table_id = intdecode(&msg_cur, msg_end);
1188 if (!msg_cur || (msg_cur + sizeof(update) > msg_end)) {
1189 /* malformed message */
1190 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1191 goto switchstate;
1192 }
1193 memcpy(&update, msg_cur, sizeof(update));
1194 update = ntohl(update);
Emeric Brun2b920a12010-09-23 18:30:22 +02001195
Emeric Brunb3971ab2015-05-12 18:49:09 +02001196 for (st = curpeer->tables; st; st = st->next) {
1197 if (st->local_id == table_id) {
1198 st->update = update;
1199 break;
1200 }
1201 }
1202 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001203 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001204 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1205 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +02001206 goto switchstate;
1207 }
1208
Emeric Brunb3971ab2015-05-12 18:49:09 +02001209ignore_msg:
Emeric Brun2b920a12010-09-23 18:30:22 +02001210 /* skip consumed message */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001211 bo_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02001212 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02001213 goto switchstate;
1214
Emeric Brun2b920a12010-09-23 18:30:22 +02001215incomplete:
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001216 /* we get here when a bo_getblk() returns <= 0 in reql */
1217
Willy Tarreau72d6c162013-04-11 16:14:13 +02001218 if (reql < 0) {
1219 /* there was an error */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001220 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001221 goto switchstate;
1222 }
1223
Emeric Brun2b920a12010-09-23 18:30:22 +02001224
1225 /* Confirm finished or partial messages */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001226 while (curpeer->confirm) {
1227 unsigned char msg[2];
1228
Emeric Brun2b920a12010-09-23 18:30:22 +02001229 /* There is a confirm messages to send */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001230 msg[0] = PEER_MSG_CLASS_CONTROL;
1231 msg[1] = PEER_MSG_CTRL_RESYNCCONFIRM;
1232
1233 /* message to buffer */
1234 repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brun2b920a12010-09-23 18:30:22 +02001235 if (repl <= 0) {
1236 /* no more write possible */
1237 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001238 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001239 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02001240 goto switchstate;
1241 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001242 curpeer->confirm--;
Emeric Brun2b920a12010-09-23 18:30:22 +02001243 }
1244
Emeric Brunb3971ab2015-05-12 18:49:09 +02001245
Emeric Brun2b920a12010-09-23 18:30:22 +02001246 /* Need to request a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001247 if ((curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1248 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1249 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
1250 unsigned char msg[2];
Emeric Brun2b920a12010-09-23 18:30:22 +02001251
Emeric Brunb3971ab2015-05-12 18:49:09 +02001252 /* Current peer was elected to request a resync */
1253 msg[0] = PEER_MSG_CLASS_CONTROL;
1254 msg[1] = PEER_MSG_CTRL_RESYNCREQ;
Emeric Brun2b920a12010-09-23 18:30:22 +02001255
Emeric Brunb3971ab2015-05-12 18:49:09 +02001256 /* message to buffer */
1257 repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg));
1258 if (repl <= 0) {
1259 /* no more write possible */
1260 if (repl == -1)
1261 goto full;
1262 appctx->st0 = PEER_SESS_ST_END;
1263 goto switchstate;
1264 }
1265 peers->flags |= PEERS_F_RESYNC_PROCESS;
1266 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001267
Emeric Brunb3971ab2015-05-12 18:49:09 +02001268 /* Nothing to read, now we start to write */
Emeric Brun2b920a12010-09-23 18:30:22 +02001269
Emeric Brunb3971ab2015-05-12 18:49:09 +02001270 if (curpeer->tables) {
1271 struct shared_table *st;
1272 struct shared_table *last_local_table;
Emeric Brun2b920a12010-09-23 18:30:22 +02001273
Emeric Brunb3971ab2015-05-12 18:49:09 +02001274 last_local_table = curpeer->last_local_table;
1275 if (!last_local_table)
1276 last_local_table = curpeer->tables;
1277 st = last_local_table->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001278
Emeric Brunb3971ab2015-05-12 18:49:09 +02001279 while (1) {
1280 if (!st)
1281 st = curpeer->tables;
Emeric Brun2b920a12010-09-23 18:30:22 +02001282
Emeric Brunb3971ab2015-05-12 18:49:09 +02001283 /* It remains some updates to ack */
1284 if (st->last_get != st->last_acked) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001285 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001286
Emeric Brunb3971ab2015-05-12 18:49:09 +02001287 msglen = peer_prepare_ackmsg(st, trash.str, trash.size);
1288 if (!msglen) {
1289 /* internal error: message does not fit in trash */
1290 appctx->st0 = PEER_SESS_ST_END;
1291 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001292 }
1293
Emeric Brunb3971ab2015-05-12 18:49:09 +02001294 /* message to buffer */
1295 repl = bi_putblk(si_ic(si), trash.str, msglen);
1296 if (repl <= 0) {
1297 /* no more write possible */
1298 if (repl == -1) {
1299 goto full;
Emeric Brun2b920a12010-09-23 18:30:22 +02001300 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001301 appctx->st0 = PEER_SESS_ST_END;
1302 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001303 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001304 st->last_acked = st->last_get;
Emeric Brun2b920a12010-09-23 18:30:22 +02001305 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001306
Emeric Brunb3971ab2015-05-12 18:49:09 +02001307 if (!(curpeer->flags & PEER_F_TEACH_PROCESS)) {
1308 if (!(curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1309 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1310 struct eb32_node *eb;
1311 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001312
Emeric Brunb3971ab2015-05-12 18:49:09 +02001313 if (st != curpeer->last_local_table) {
1314 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001315
Emeric Brunb3971ab2015-05-12 18:49:09 +02001316 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1317 if (!msglen) {
1318 /* internal error: message does not fit in trash */
1319 appctx->st0 = PEER_SESS_ST_END;
1320 goto switchstate;
1321 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001322
Emeric Brunb3971ab2015-05-12 18:49:09 +02001323 /* message to buffer */
1324 repl = bi_putblk(si_ic(si), trash.str, msglen);
1325 if (repl <= 0) {
1326 /* no more write possible */
1327 if (repl == -1) {
1328 goto full;
1329 }
1330 appctx->st0 = PEER_SESS_ST_END;
1331 goto switchstate;
1332 }
1333 curpeer->last_local_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001334 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001335
1336 /* We force new pushed to 1 to force identifier in update message */
1337 new_pushed = 1;
1338 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1339 while (1) {
1340 uint32_t msglen;
1341 struct stksess *ts;
1342
1343 /* push local updates */
1344 if (!eb) {
1345 eb = eb32_first(&st->table->updates);
1346 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001347 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001348 break;
1349 }
1350 }
1351
1352 if ((int)(eb->key - st->table->localupdate) > 0) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001353 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001354 break;
1355 }
1356
1357 ts = eb32_entry(eb, struct stksess, upd);
1358 msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed);
1359 if (!msglen) {
1360 /* internal error: message does not fit in trash */
1361 appctx->st0 = PEER_SESS_ST_END;
1362 goto switchstate;
1363 }
1364
1365 /* message to buffer */
1366 repl = bi_putblk(si_ic(si), trash.str, msglen);
1367 if (repl <= 0) {
1368 /* no more write possible */
1369 if (repl == -1) {
1370 goto full;
1371 }
1372 appctx->st0 = PEER_SESS_ST_END;
1373 goto switchstate;
1374 }
1375 st->last_pushed = ts->upd.key;
Emeric Brunaaf58602015-06-15 17:23:30 +02001376 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
1377 st->table->commitupdate = st->last_pushed;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001378 /* identifier may not needed in next update message */
1379 new_pushed = 0;
1380
1381 eb = eb32_next(eb);
1382 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001383 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001384 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001385 else {
1386 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1387 struct eb32_node *eb;
1388 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001389
Emeric Brunb3971ab2015-05-12 18:49:09 +02001390 if (st != curpeer->last_local_table) {
1391 int msglen;
1392
1393 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1394 if (!msglen) {
1395 /* internal error: message does not fit in trash */
1396 appctx->st0 = PEER_SESS_ST_END;
1397 goto switchstate;
1398 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001399
Emeric Brunb3971ab2015-05-12 18:49:09 +02001400 /* message to buffer */
1401 repl = bi_putblk(si_ic(si), trash.str, msglen);
1402 if (repl <= 0) {
1403 /* no more write possible */
1404 if (repl == -1) {
1405 goto full;
1406 }
1407 appctx->st0 = PEER_SESS_ST_END;
1408 goto switchstate;
1409 }
1410 curpeer->last_local_table = st;
1411 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001412
Emeric Brunb3971ab2015-05-12 18:49:09 +02001413 /* We force new pushed to 1 to force identifier in update message */
1414 new_pushed = 1;
1415 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1416 while (1) {
1417 uint32_t msglen;
1418 struct stksess *ts;
Emeric Brun2b920a12010-09-23 18:30:22 +02001419
Emeric Brunb3971ab2015-05-12 18:49:09 +02001420 /* push local updates */
1421 if (!eb) {
1422 st->flags |= SHTABLE_F_TEACH_STAGE1;
1423 eb = eb32_first(&st->table->updates);
1424 if (eb)
1425 st->last_pushed = eb->key - 1;
1426 break;
1427 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001428
Emeric Brunb3971ab2015-05-12 18:49:09 +02001429 ts = eb32_entry(eb, struct stksess, upd);
1430 msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed);
1431 if (!msglen) {
1432 /* internal error: message does not fit in trash */
1433 appctx->st0 = PEER_SESS_ST_END;
1434 goto switchstate;
1435 }
1436
1437 /* message to buffer */
1438 repl = bi_putblk(si_ic(si), trash.str, msglen);
1439 if (repl <= 0) {
1440 /* no more write possible */
1441 if (repl == -1) {
1442 goto full;
1443 }
1444 appctx->st0 = PEER_SESS_ST_END;
1445 goto switchstate;
1446 }
1447 st->last_pushed = ts->upd.key;
1448 /* identifier may not needed in next update message */
1449 new_pushed = 0;
1450
1451 eb = eb32_next(eb);
1452 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001453 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001454
Emeric Brunb3971ab2015-05-12 18:49:09 +02001455 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1456 struct eb32_node *eb;
1457 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001458
Emeric Brunb3971ab2015-05-12 18:49:09 +02001459 if (st != curpeer->last_local_table) {
1460 int msglen;
1461
1462 msglen = peer_prepare_switchmsg(st, trash.str, trash.size);
1463 if (!msglen) {
1464 /* internal error: message does not fit in trash */
1465 appctx->st0 = PEER_SESS_ST_END;
1466 goto switchstate;
1467 }
1468
1469 /* message to buffer */
1470 repl = bi_putblk(si_ic(si), trash.str, msglen);
1471 if (repl <= 0) {
1472 /* no more write possible */
1473 if (repl == -1) {
1474 goto full;
1475 }
1476 appctx->st0 = PEER_SESS_ST_END;
1477 goto switchstate;
1478 }
1479 curpeer->last_local_table = st;
1480 }
1481
1482 /* We force new pushed to 1 to force identifier in update message */
1483 new_pushed = 1;
1484 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1485 while (1) {
1486 uint32_t msglen;
1487 struct stksess *ts;
1488
1489 /* push local updates */
1490 if (!eb || eb->key > st->teaching_origin) {
1491 st->flags |= SHTABLE_F_TEACH_STAGE2;
1492 eb = eb32_first(&st->table->updates);
1493 if (eb)
1494 st->last_pushed = eb->key - 1;
1495 break;
1496 }
1497
1498 ts = eb32_entry(eb, struct stksess, upd);
1499 msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed);
1500 if (!msglen) {
1501 /* internal error: message does not fit in trash */
1502 appctx->st0 = PEER_SESS_ST_END;
1503 goto switchstate;
1504 }
1505
1506 /* message to buffer */
1507 repl = bi_putblk(si_ic(si), trash.str, msglen);
1508 if (repl <= 0) {
1509 /* no more write possible */
1510 if (repl == -1) {
1511 goto full;
1512 }
1513 appctx->st0 = PEER_SESS_ST_END;
1514 goto switchstate;
1515 }
1516 st->last_pushed = ts->upd.key;
1517 /* identifier may not needed in next update message */
1518 new_pushed = 0;
1519
1520 eb = eb32_next(eb);
1521 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001522 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001523 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001524
1525 if (st == last_local_table)
1526 break;
1527 st = st->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001528 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001529 }
1530
1531
1532 if ((curpeer->flags & PEER_F_TEACH_PROCESS) && !(curpeer->flags & PEER_F_TEACH_FINISHED)) {
1533 unsigned char msg[2];
1534
1535 /* Current peer was elected to request a resync */
1536 msg[0] = PEER_MSG_CLASS_CONTROL;
1537 msg[1] = ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED) ? PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
1538 /* process final lesson message */
1539 repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg));
1540 if (repl <= 0) {
1541 /* no more write possible */
1542 if (repl == -1)
1543 goto full;
1544 appctx->st0 = PEER_SESS_ST_END;
1545 goto switchstate;
1546 }
1547 /* flag finished message sent */
1548 curpeer->flags |= PEER_F_TEACH_FINISHED;
1549 }
1550
Emeric Brun2b920a12010-09-23 18:30:22 +02001551 /* noting more to do */
1552 goto out;
1553 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001554 case PEER_SESS_ST_EXIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001555 repl = snprintf(trash.str, trash.size, "%d\n", appctx->st1);
Emeric Brun2b920a12010-09-23 18:30:22 +02001556
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001557 if (bi_putblk(si_ic(si), trash.str, repl) == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001558 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001559 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001560 goto switchstate;
1561 case PEER_SESS_ST_ERRSIZE: {
1562 unsigned char msg[2];
1563
1564 msg[0] = PEER_MSG_CLASS_ERROR;
1565 msg[1] = PEER_MSG_ERR_SIZELIMIT;
1566
1567 if (bi_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
1568 goto full;
1569 appctx->st0 = PEER_SESS_ST_END;
1570 goto switchstate;
1571 }
1572 case PEER_SESS_ST_ERRPROTO: {
1573 unsigned char msg[2];
1574
1575 msg[0] = PEER_MSG_CLASS_ERROR;
1576 msg[1] = PEER_MSG_ERR_PROTOCOL;
1577
1578 if (bi_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
1579 goto full;
1580 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02001581 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001582 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001583 case PEER_SESS_ST_END: {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001584 si_shutw(si);
1585 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001586 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02001587 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001588 }
1589 }
1590 }
1591out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001592 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun2b920a12010-09-23 18:30:22 +02001593 return;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001594full:
Willy Tarreaufe127932015-04-21 19:23:39 +02001595 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001596 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001597}
1598
Willy Tarreau30576452015-04-13 13:50:30 +02001599static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001600 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001601 .name = "<PEER>", /* used for logging */
1602 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07001603 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001604};
Emeric Brun2b920a12010-09-23 18:30:22 +02001605
1606/*
1607 * Use this function to force a close of a peer session
1608 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001609static void peer_session_forceshutdown(struct stream * stream)
Emeric Brun2b920a12010-09-23 18:30:22 +02001610{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001611 struct appctx *appctx = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001612 struct peer *ps;
1613
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001614 int i;
Emeric Brun2b920a12010-09-23 18:30:22 +02001615
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001616 for (i = 0; i <= 1; i++) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001617 appctx = objt_appctx(stream->si[i].end);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001618 if (!appctx)
1619 continue;
1620 if (appctx->applet != &peer_applet)
1621 continue;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001622 break;
Emeric Brun2b920a12010-09-23 18:30:22 +02001623 }
1624
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001625 if (!appctx)
1626 return;
1627
Emeric Brunb3971ab2015-05-12 18:49:09 +02001628 ps = (struct peer *)appctx->ctx.peers.ptr;
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001629 /* we're killing a connection, we must apply a random delay before
1630 * retrying otherwise the other end will do the same and we can loop
1631 * for a while.
1632 */
1633 if (ps)
1634 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
1635
Emeric Brun2b920a12010-09-23 18:30:22 +02001636 /* call release to reinit resync states if needed */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001637 peer_session_release(appctx);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001638 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001639 appctx->ctx.peers.ptr = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001640 task_wakeup(stream->task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02001641}
1642
Willy Tarreau91d96282015-03-13 15:47:26 +01001643/* Pre-configures a peers frontend to accept incoming connections */
1644void peers_setup_frontend(struct proxy *fe)
1645{
1646 fe->last_change = now.tv_sec;
1647 fe->cap = PR_CAP_FE;
1648 fe->maxconn = 0;
1649 fe->conn_retries = CONN_RETRIES;
1650 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01001651 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01001652 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01001653 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02001654 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01001655}
1656
Emeric Brun2b920a12010-09-23 18:30:22 +02001657/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01001658 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02001659 */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001660static struct stream *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02001661{
Emeric Brunb3971ab2015-05-12 18:49:09 +02001662 struct listener *l = LIST_NEXT(&peers->peers_fe->conf.listeners, struct listener *, by_fe);
Emeric Brun2b920a12010-09-23 18:30:22 +02001663 struct proxy *p = (struct proxy *)l->frontend; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001664 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02001665 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02001666 struct stream *s;
Emeric Brun2b920a12010-09-23 18:30:22 +02001667 struct task *t;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001668 struct connection *conn;
Emeric Brun2b920a12010-09-23 18:30:22 +02001669
Emeric Brunb3971ab2015-05-12 18:49:09 +02001670 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
1671 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001672 s = NULL;
1673
1674 appctx = appctx_new(&peer_applet);
1675 if (!appctx)
1676 goto out_close;
1677
1678 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001679 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001680
Willy Tarreau4099a7c2015-04-05 00:39:55 +02001681 sess = session_new(p, l, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001682 if (!sess) {
Godbach430f2912013-06-20 13:28:38 +08001683 Alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02001684 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001685 }
1686
Willy Tarreau8baf9062015-04-05 00:46:36 +02001687 if ((t = task_new()) == NULL) {
Willy Tarreau15b5e142015-04-04 14:38:25 +02001688 Alert("out of memory in peer_session_create().\n");
1689 goto out_free_sess;
1690 }
Willy Tarreau8baf9062015-04-05 00:46:36 +02001691 t->nice = l->nice;
1692
Willy Tarreau73b65ac2015-04-08 18:26:29 +02001693 if ((s = stream_new(sess, t, &appctx->obj_type)) == NULL) {
Willy Tarreau342bfb12015-04-05 01:35:34 +02001694 Alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau8baf9062015-04-05 00:46:36 +02001695 goto out_free_task;
1696 }
1697
Willy Tarreau342bfb12015-04-05 01:35:34 +02001698 /* The tasks below are normally what is supposed to be done by
1699 * fe->accept().
1700 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001701 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001702
Willy Tarreau6e2979c2015-04-27 13:21:15 +02001703 /* applet is waiting for data */
1704 si_applet_cant_get(&s->si[0]);
1705 appctx_wakeup(appctx);
1706
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001707 /* initiate an outgoing connection */
1708 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001709
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001710 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001711 * pre-initialized connection in si->conn.
1712 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001713 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02001714 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001715
1716 conn_prepare(conn, peer->proto, peer->xprt);
1717 si_attach_conn(&s->si[1], conn);
1718
1719 conn->target = s->target = &s->be->obj_type;
1720 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
Emeric Brun2b920a12010-09-23 18:30:22 +02001721 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001722 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001723
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001724 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01001725
Emeric Brun2b920a12010-09-23 18:30:22 +02001726 l->nbconn++; /* warning! right now, it's up to the handler to decrease this */
1727 p->feconn++;/* beconn will be increased later */
1728 jobs++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001729 if (!(s->sess->listener->options & LI_O_UNLIMITED))
Willy Tarreau3c63fd82011-09-07 18:00:47 +02001730 actconn++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001731 totalconn++;
1732
Emeric Brunb3971ab2015-05-12 18:49:09 +02001733 peer->appctx = appctx;
1734 peer->stream = s;
Emeric Brun2b920a12010-09-23 18:30:22 +02001735 return s;
1736
1737 /* Error unrolling */
Willy Tarreau15b5e142015-04-04 14:38:25 +02001738 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02001739 LIST_DEL(&s->list);
Willy Tarreau87b09662015-04-03 00:22:06 +02001740 pool_free2(pool2_stream, s);
Willy Tarreau8baf9062015-04-05 00:46:36 +02001741 out_free_task:
1742 task_free(t);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001743 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02001744 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02001745 out_free_appctx:
1746 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001747 out_close:
1748 return s;
1749}
1750
1751/*
1752 * Task processing function to manage re-connect and peer session
1753 * tasks wakeup on local update.
1754 */
Simon Horman96553772011-06-08 09:18:51 +09001755static struct task *process_peer_sync(struct task * task)
Emeric Brun2b920a12010-09-23 18:30:22 +02001756{
Emeric Brunb3971ab2015-05-12 18:49:09 +02001757 struct peers *peers = (struct peers *)task->context;
1758 struct peer *ps;
1759 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001760
1761 task->expire = TICK_ETERNITY;
1762
Emeric Brunb3971ab2015-05-12 18:49:09 +02001763 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02001764 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001765 signal_unregister_handler(peers->sighandler);
1766 peers->sync_task = NULL;
1767 task_delete(peers->sync_task);
1768 task_free(peers->sync_task);
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02001769 return NULL;
1770 }
1771
Emeric Brun2b920a12010-09-23 18:30:22 +02001772 if (!stopping) {
1773 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02001774
1775 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
1776 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
1777 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001778 /* Resync from local peer needed
1779 no peer was assigned for the lesson
1780 and no old local peer found
1781 or resync timeout expire */
1782
1783 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001784 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001785
1786 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001787 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +02001788 }
1789
1790 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001791 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001792 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001793 if (!ps->local) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001794 if (!ps->stream) {
1795 /* no active stream */
Emeric Brun2b920a12010-09-23 18:30:22 +02001796 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001797 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001798 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001799 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02001800 tick_is_expired(ps->reconnect, now_ms))) {
1801 /* connection never tried
Willy Tarreau87b09662015-04-03 00:22:06 +02001802 * or previous stream established with success
1803 * or previous stream failed during connection
Emeric Brun2b920a12010-09-23 18:30:22 +02001804 * and reconnection timer is expired */
1805
1806 /* retry a connect */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001807 ps->stream = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02001808 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02001809 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001810 /* If previous session failed during connection
1811 * but reconnection timer is not expired */
1812
1813 /* reschedule task for reconnect */
1814 task->expire = tick_first(task->expire, ps->reconnect);
1815 }
1816 /* else do nothing */
Willy Tarreau87b09662015-04-03 00:22:06 +02001817 } /* !ps->stream */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001818 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001819 /* current stream is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001820 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
1821 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02001822 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
1823 /* Resync from a remote is needed
1824 * and no peer was assigned for lesson
1825 * and current peer may be up2date */
1826
1827 /* assign peer for the lesson */
1828 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001829 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02001830
Willy Tarreau87b09662015-04-03 00:22:06 +02001831 /* awake peer stream task to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02001832 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001833 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001834 else {
1835 /* Awake session if there is data to push */
1836 for (st = ps->tables; st ; st = st->next) {
1837 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
1838 /* awake peer stream task to push local updates */
1839 appctx_wakeup(ps->appctx);
1840 break;
1841 }
1842 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001843 }
1844 /* else do nothing */
1845 } /* SUCCESSCODE */
1846 } /* !ps->peer->local */
1847 } /* for */
1848
1849 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001850 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
1851 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1852 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001853 /* Resync from remote peer needed
1854 * no peer was assigned for the lesson
1855 * and resync timeout expire */
1856
1857 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001858 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02001859 }
1860
Emeric Brunb3971ab2015-05-12 18:49:09 +02001861 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001862 /* Resync not finished*/
1863 /* reschedule task to resync timeout, to ended resync if needed */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001864 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02001865 }
1866 } /* !stopping */
1867 else {
1868 /* soft stop case */
1869 if (task->state & TASK_WOKEN_SIGNAL) {
1870 /* We've just recieved the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001871 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001872 /* add DO NOT STOP flag if not present */
1873 jobs++;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001874 peers->flags |= PEERS_F_DONOTSTOP;
1875 ps = peers->local;
1876 for (st = ps->tables; st ; st = st->next)
1877 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001878 }
1879
1880 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001881 for (ps = peers->remote; ps; ps = ps->next) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001882 if (ps->stream) {
1883 peer_session_forceshutdown(ps->stream);
1884 ps->stream = NULL;
Willy Tarreaue5843b32015-04-27 18:40:14 +02001885 ps->appctx = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001886 }
1887 }
1888 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001889
Emeric Brunb3971ab2015-05-12 18:49:09 +02001890 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02001891 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001892 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001893 /* resync of new process was complete, current process can die now */
1894 jobs--;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001895 peers->flags &= ~PEERS_F_DONOTSTOP;
1896 for (st = ps->tables; st ; st = st->next)
1897 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02001898 }
1899 }
Willy Tarreau87b09662015-04-03 00:22:06 +02001900 else if (!ps->stream) {
1901 /* If stream is not active */
Emeric Brun2b920a12010-09-23 18:30:22 +02001902 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001903 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
1904 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
1905 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001906 /* connection never tried
Willy Tarreau87b09662015-04-03 00:22:06 +02001907 * or previous stream was successfully established
1908 * or previous stream tcp connect success but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02001909 * or during previous connect, peer replies a try again statuscode */
1910
1911 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001912 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02001913 }
1914 else {
1915 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001916 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001917 /* unable to resync new process, current process can die now */
1918 jobs--;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001919 peers->flags &= ~PEERS_F_DONOTSTOP;
1920 for (st = ps->tables; st ; st = st->next)
1921 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02001922 }
1923 }
1924 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001925 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001926 /* current stream active and established
1927 awake stream to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001928 for (st = ps->tables; st ; st = st->next) {
1929 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
1930 /* awake peer stream task to push local updates */
1931 appctx_wakeup(ps->appctx);
1932 break;
1933 }
1934 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001935 }
1936 } /* stopping */
1937 /* Wakeup for re-connect */
1938 return task;
1939}
1940
Emeric Brunb3971ab2015-05-12 18:49:09 +02001941
Emeric Brun2b920a12010-09-23 18:30:22 +02001942/*
Emeric Brun2b920a12010-09-23 18:30:22 +02001943 *
1944 */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001945void peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02001946{
Emeric Brun2b920a12010-09-23 18:30:22 +02001947 struct peer * curpeer;
Willy Tarreau4348fad2012-09-20 16:48:07 +02001948 struct listener *listener;
Emeric Brun2b920a12010-09-23 18:30:22 +02001949
Emeric Brun2b920a12010-09-23 18:30:22 +02001950 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001951 peers->peers_fe->maxconn += 3;
1952 }
1953
Willy Tarreau4348fad2012-09-20 16:48:07 +02001954 list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe)
1955 listener->maxconn = peers->peers_fe->maxconn;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001956 peers->sync_task = task_new();
1957 peers->sync_task->process = process_peer_sync;
1958 peers->sync_task->expire = TICK_ETERNITY;
1959 peers->sync_task->context = (void *)peers;
1960 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
1961 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
1962}
1963
1964
1965
1966/*
1967 * Function used to register a table for sync on a group of peers
1968 *
1969 */
1970void peers_register_table(struct peers *peers, struct stktable *table)
1971{
1972 struct shared_table *st;
1973 struct peer * curpeer;
1974 int id = 0;
1975
1976 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
1977 st = (struct shared_table *)calloc(1,sizeof(struct shared_table));
1978 st->table = table;
1979 st->next = curpeer->tables;
1980 if (curpeer->tables)
1981 id = curpeer->tables->local_id;
1982 st->local_id = id + 1;
1983
1984 curpeer->tables = st;
1985 }
1986
1987 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02001988}
1989