blob: d4d3859e3d65e25935a97b7a59247103cd78a7fb [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>
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +020026#include <common/standard.h>
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020027#include <common/hathreads.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020028
29#include <types/global.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010030#include <types/listener.h>
31#include <types/obj_type.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020032#include <types/peers.h>
33
34#include <proto/acl.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020035#include <proto/applet.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020036#include <proto/channel.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020037#include <proto/fd.h>
Willy Tarreaud1d48d42015-03-13 16:15:46 +010038#include <proto/frontend.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020039#include <proto/log.h>
40#include <proto/hdr_idx.h>
Willy Tarreau53a47662017-08-28 10:53:00 +020041#include <proto/mux_pt.h>
Frédéric Lécaille1055e682018-04-26 14:35:21 +020042#include <proto/peers.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020043#include <proto/proxy.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020044#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020045#include <proto/stream.h>
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010046#include <proto/signal.h>
47#include <proto/stick_table.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020048#include <proto/stream_interface.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020049#include <proto/task.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020050
51
52/*******************************/
53/* Current peer learning state */
54/*******************************/
55
56/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020057/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020058/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020059#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
60#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
61#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
62#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
63#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
Emeric Brun2b920a12010-09-23 18:30:22 +020064 to push data to new process */
65
Emeric Brunb3971ab2015-05-12 18:49:09 +020066#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
67#define PEERS_RESYNC_FROMLOCAL 0x00000000
68#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
69#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
70
71/***********************************/
72/* Current shared table sync state */
73/***********************************/
74#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
75#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020076
77/******************************/
78/* Remote peer teaching state */
79/******************************/
80#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
Emeric Brun2b920a12010-09-23 18:30:22 +020081#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
82#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
83#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
84#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +020085#define PEER_F_DWNGRD 0x80000000 /* When this flag is enabled, we must downgrade the supported version announced during peer sessions. */
Emeric Brun2b920a12010-09-23 18:30:22 +020086
Emeric Brunb3971ab2015-05-12 18:49:09 +020087#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 +020088#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
89
Emeric Brunb3971ab2015-05-12 18:49:09 +020090/*****************************/
91/* Sync message class */
92/*****************************/
93enum {
94 PEER_MSG_CLASS_CONTROL = 0,
95 PEER_MSG_CLASS_ERROR,
96 PEER_MSG_CLASS_STICKTABLE = 10,
97 PEER_MSG_CLASS_RESERVED = 255,
98};
99
100/*****************************/
101/* control message types */
102/*****************************/
103enum {
104 PEER_MSG_CTRL_RESYNCREQ = 0,
105 PEER_MSG_CTRL_RESYNCFINISHED,
106 PEER_MSG_CTRL_RESYNCPARTIAL,
107 PEER_MSG_CTRL_RESYNCCONFIRM,
108};
109
110/*****************************/
111/* error message types */
112/*****************************/
113enum {
114 PEER_MSG_ERR_PROTOCOL = 0,
115 PEER_MSG_ERR_SIZELIMIT,
116};
117
118
119/*******************************/
120/* stick table sync mesg types */
121/* Note: ids >= 128 contains */
122/* id message cotains data */
123/*******************************/
Olivier Houchard33992262018-10-16 18:49:26 +0200124#define PEER_MSG_STKT_UPDATE 0x80
125#define PEER_MSG_STKT_INCUPDATE 0x81
126#define PEER_MSG_STKT_DEFINE 0x82
127#define PEER_MSG_STKT_SWITCH 0x83
128#define PEER_MSG_STKT_ACK 0x84
129#define PEER_MSG_STKT_UPDATE_TIMED 0x85
130#define PEER_MSG_STKT_INCUPDATE_TIMED 0x86
Emeric Brun2b920a12010-09-23 18:30:22 +0200131
132/**********************************/
133/* Peer Session IO handler states */
134/**********************************/
135
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100136enum {
137 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
138 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
139 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
140 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100141 /* after this point, data were possibly exchanged */
142 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
143 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
144 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
145 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
146 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200147 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
148 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100149 PEER_SESS_ST_END, /* Killed session */
150};
Emeric Brun2b920a12010-09-23 18:30:22 +0200151
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100152/***************************************************/
153/* Peer Session status code - part of the protocol */
154/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200155
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100156#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
157#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200158
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100159#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200160
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100161#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200162
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100163#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
164#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
165#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
166#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200167
168#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200169#define PEER_MAJOR_VER 2
170#define PEER_MINOR_VER 1
171#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200172
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200173struct peers *cfg_peers = NULL;
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100174static void peer_session_forceshutdown(struct appctx *appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200175
Emeric Brun18928af2017-03-29 16:32:53 +0200176/* This function encode an uint64 to 'dynamic' length format.
177 The encoded value is written at address *str, and the
178 caller must assure that size after *str is large enought.
179 At return, the *str is set at the next Byte after then
180 encoded integer. The function returns then length of the
181 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200182int intencode(uint64_t i, char **str) {
183 int idx = 0;
184 unsigned char *msg;
185
186 if (!*str)
187 return 0;
188
189 msg = (unsigned char *)*str;
190 if (i < 240) {
191 msg[0] = (unsigned char)i;
192 *str = (char *)&msg[idx+1];
193 return (idx+1);
194 }
195
196 msg[idx] =(unsigned char)i | 240;
197 i = (i - 240) >> 4;
198 while (i >= 128) {
199 msg[++idx] = (unsigned char)i | 128;
200 i = (i - 128) >> 7;
201 }
202 msg[++idx] = (unsigned char)i;
203 *str = (char *)&msg[idx+1];
204 return (idx+1);
205}
206
207
208/* This function returns the decoded integer or 0
209 if decode failed
210 *str point on the beginning of the integer to decode
211 at the end of decoding *str point on the end of the
212 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200213uint64_t intdecode(char **str, char *end)
214{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200215 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200216 uint64_t i;
217 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200218
219 if (!*str)
220 return 0;
221
222 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200223 if (msg >= (unsigned char *)end)
224 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200225
Emeric Brun18928af2017-03-29 16:32:53 +0200226 i = *(msg++);
227 if (i >= 240) {
228 shift = 4;
229 do {
230 if (msg >= (unsigned char *)end)
231 goto fail;
232 i += (uint64_t)*msg << shift;
233 shift += 7;
234 } while (*(msg++) >= 128);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200235 }
Emeric Brun18928af2017-03-29 16:32:53 +0200236 *str = (char *)msg;
237 return i;
238
239 fail:
240 *str = NULL;
241 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200242}
Emeric Brun2b920a12010-09-23 18:30:22 +0200243
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200244/* Set the stick-table UPDATE message type byte at <msg_type> address,
245 * depending on <use_identifier> and <use_timed> boolean parameters.
246 * Always successful.
247 */
248static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
249{
250 if (use_timed) {
251 if (use_identifier)
252 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
253 else
254 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
255 }
256 else {
257 if (use_identifier)
258 *msg_type = PEER_MSG_STKT_UPDATE;
259 else
260 *msg_type = PEER_MSG_STKT_INCUPDATE;
261 }
262
263}
Emeric Brun2b920a12010-09-23 18:30:22 +0200264/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200265 * This prepare the data update message on the stick session <ts>, <st> is the considered
266 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800267 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200268 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
269 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200270 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200271static int peer_prepare_updatemsg(struct stksess *ts, struct shared_table *st, unsigned int updateid, char *msg, size_t size, int use_identifier, int use_timed)
Emeric Brun2b920a12010-09-23 18:30:22 +0200272{
273 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200274 unsigned short datalen;
275 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200276 unsigned int data_type;
277 void *data_ptr;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200278
279 cursor = datamsg = msg + 1 + 5;
280
Emeric Brun2b920a12010-09-23 18:30:22 +0200281 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200282
283 /* check if we need to send the update identifer */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200284 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200285 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200286 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200287
288 /* encode update identifier if needed */
289 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200290 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200291 memcpy(cursor, &netinteger, sizeof(netinteger));
292 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200293 }
294
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200295 if (use_timed) {
296 netinteger = htonl(tick_remain(now_ms, ts->expire));
297 memcpy(cursor, &netinteger, sizeof(netinteger));
298 cursor += sizeof(netinteger);
299 }
300
Emeric Brunb3971ab2015-05-12 18:49:09 +0200301 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200302 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200303 int stlen = strlen((char *)ts->key.key);
304
Emeric Brunb3971ab2015-05-12 18:49:09 +0200305 intencode(stlen, &cursor);
306 memcpy(cursor, ts->key.key, stlen);
307 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200308 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200309 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200310 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200311 memcpy(cursor, &netinteger, sizeof(netinteger));
312 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200313 }
314 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200315 memcpy(cursor, ts->key.key, st->table->key_size);
316 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200317 }
318
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100319 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200320 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200321 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200322
Emeric Brun94900952015-06-11 18:25:54 +0200323 data_ptr = stktable_data_ptr(st->table, ts, data_type);
324 if (data_ptr) {
325 switch (stktable_data_types[data_type].std_type) {
326 case STD_T_SINT: {
327 int data;
328
329 data = stktable_data_cast(data_ptr, std_t_sint);
330 intencode(data, &cursor);
331 break;
332 }
333 case STD_T_UINT: {
334 unsigned int data;
335
336 data = stktable_data_cast(data_ptr, std_t_uint);
337 intencode(data, &cursor);
338 break;
339 }
340 case STD_T_ULL: {
341 unsigned long long data;
342
343 data = stktable_data_cast(data_ptr, std_t_ull);
344 intencode(data, &cursor);
345 break;
346 }
347 case STD_T_FRQP: {
348 struct freq_ctr_period *frqp;
349
350 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
351 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
352 intencode(frqp->curr_ctr, &cursor);
353 intencode(frqp->prev_ctr, &cursor);
354 break;
355 }
356 }
357 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200358 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100359 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200360
361 /* Compute datalen */
362 datalen = (cursor - datamsg);
363
364 /* prepare message header */
365 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200366 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200367 cursor = &msg[2];
368 intencode(datalen, &cursor);
369
370 /* move data after header */
371 memmove(cursor, datamsg, datalen);
372
373 /* return header size + data_len */
374 return (cursor - msg) + datalen;
375}
376
377/*
378 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800379 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200380 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
381 * check size)
382 */
383static int peer_prepare_switchmsg(struct shared_table *st, char *msg, size_t size)
384{
385 int len;
386 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200387 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200388 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200389 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200390 unsigned int data_type;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200391
392 cursor = datamsg = msg + 2 + 5;
393
394 /* Encode data */
395
396 /* encode local id */
397 intencode(st->local_id, &cursor);
398
399 /* encode table name */
400 len = strlen(st->table->id);
401 intencode(len, &cursor);
402 memcpy(cursor, st->table->id, len);
403 cursor += len;
404
405 /* encode table type */
406
407 intencode(st->table->type, &cursor);
408
409 /* encode table key size */
410 intencode(st->table->key_size, &cursor);
411
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200412 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200413 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200414 /* encode available known data types in table */
415 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
416 if (st->table->data_ofs[data_type]) {
417 switch (stktable_data_types[data_type].std_type) {
418 case STD_T_SINT:
419 case STD_T_UINT:
420 case STD_T_ULL:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200421 data |= 1 << data_type;
422 break;
Emeric Brun94900952015-06-11 18:25:54 +0200423 case STD_T_FRQP:
424 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200425 intencode(data_type, &chunkq);
426 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200427 break;
428 }
429 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200430 }
431 intencode(data, &cursor);
432
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200433 /* Encode stick-table entries duration. */
434 intencode(st->table->expire, &cursor);
435
436 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200437 chunk->data = chunkq - chunkp;
438 memcpy(cursor, chunk->area, chunk->data);
439 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200440 }
441
Emeric Brunb3971ab2015-05-12 18:49:09 +0200442 /* Compute datalen */
443 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200444
Emeric Brunb3971ab2015-05-12 18:49:09 +0200445 /* prepare message header */
446 msg[0] = PEER_MSG_CLASS_STICKTABLE;
447 msg[1] = PEER_MSG_STKT_DEFINE;
448 cursor = &msg[2];
449 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200450
Emeric Brunb3971ab2015-05-12 18:49:09 +0200451 /* move data after header */
452 memmove(cursor, datamsg, datalen);
453
454 /* return header size + data_len */
455 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200456}
457
Emeric Brunb3971ab2015-05-12 18:49:09 +0200458/*
459 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
460 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800461 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200462 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
463 * check size)
464 */
465static int peer_prepare_ackmsg(struct shared_table *st, char *msg, size_t size)
466{
467 unsigned short datalen;
468 char *cursor, *datamsg;
469 uint32_t netinteger;
470
Emeric Brunb058f1c2015-09-22 15:50:18 +0200471 cursor = datamsg = msg + 2 + 5;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200472
473 intencode(st->remote_id, &cursor);
474 netinteger = htonl(st->last_get);
475 memcpy(cursor, &netinteger, sizeof(netinteger));
476 cursor += sizeof(netinteger);
477
478 /* Compute datalen */
479 datalen = (cursor - datamsg);
480
481 /* prepare message header */
482 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200483 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200484 cursor = &msg[2];
485 intencode(datalen, &cursor);
486
487 /* move data after header */
488 memmove(cursor, datamsg, datalen);
489
490 /* return header size + data_len */
491 return (cursor - msg) + datalen;
492}
Emeric Brun2b920a12010-09-23 18:30:22 +0200493
494/*
495 * Callback to release a session with a peer
496 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200497static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200498{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200499 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200500 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200501 struct peer *peer = appctx->ctx.peers.ptr;
502 struct peers *peers = strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200503
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100504 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100505 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200506 return;
507
508 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200509 if (peer) {
Willy Tarreau2d372c22018-11-05 17:12:27 +0100510 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
511 HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau199ad242018-11-05 16:31:22 +0100512 HA_ATOMIC_SUB(&active_peers, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100513 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +0100514 if (peer->appctx == appctx) {
Emeric Brunb157d732015-08-21 12:00:30 +0200515 /* Re-init current table pointers to force announcement on re-connect */
516 peer->remote_table = peer->last_local_table = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200517 peer->appctx = NULL;
518 if (peer->flags & PEER_F_LEARN_ASSIGN) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200519 /* unassign current peer for learning */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200520 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
521 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brun2b920a12010-09-23 18:30:22 +0200522
523 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200524 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +0200525 }
526 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200527 peer->flags &= PEER_TEACH_RESET;
528 peer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200529 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100530 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200531 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200532 }
533}
534
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200535/* Retrieve the major and minor versions of peers protocol
536 * announced by a remote peer. <str> is a null-terminated
537 * string with the following format: "<maj_ver>.<min_ver>".
538 */
539static int peer_get_version(const char *str,
540 unsigned int *maj_ver, unsigned int *min_ver)
541{
542 unsigned int majv, minv;
543 const char *pos, *saved;
544 const char *end;
545
546 saved = pos = str;
547 end = str + strlen(str);
548
549 majv = read_uint(&pos, end);
550 if (saved == pos || *pos++ != '.')
551 return -1;
552
553 saved = pos;
554 minv = read_uint(&pos, end);
555 if (saved == pos || pos != end)
556 return -1;
557
558 *maj_ver = majv;
559 *min_ver = minv;
560
561 return 0;
562}
Emeric Brun2b920a12010-09-23 18:30:22 +0200563
564/*
565 * IO Handler to handle message exchance with a peer
566 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200567static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200568{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200569 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200570 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200571 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +0200572 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +0200573 int reql = 0;
574 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200575 size_t proto_len = strlen(PEER_SESSION_PROTO_NAME);
576 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +0100577 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +0200578
Joseph Herlant82b2f542018-11-15 12:19:14 -0800579 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200580 if (si_ic(si)->buf.size == 0)
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100581 goto full;
582
Emeric Brun2b920a12010-09-23 18:30:22 +0200583 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +0100584 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200585switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200586 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100587 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100588 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +0100589 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100590 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100591 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200592 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100593 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +0100594 prev_state = appctx->st0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200595 reql = co_getline(si_oc(si), trash.area,
596 trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200597 if (reql <= 0) { /* closed or EOL not found */
598 if (reql == 0)
599 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100600 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200601 goto switchstate;
602 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200603 if (trash.area[reql-1] != '\n') {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100604 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200605 goto switchstate;
606 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200607 else if (reql > 1 && (trash.area[reql-2] == '\r'))
608 trash.area[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200609 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200610 trash.area[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200611
Willy Tarreau06d80a92017-10-19 14:32:15 +0200612 co_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200613
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200614 /* test protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200615 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200616 appctx->st0 = PEER_SESS_ST_EXIT;
617 appctx->st1 = PEER_SESS_SC_ERRPROTO;
618 goto switchstate;
619 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200620 if (peer_get_version(trash.area + proto_len + 1, &maj_ver, &min_ver) == -1 ||
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200621 maj_ver != PEER_MAJOR_VER || min_ver > PEER_MINOR_VER) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100622 appctx->st0 = PEER_SESS_ST_EXIT;
623 appctx->st1 = PEER_SESS_SC_ERRVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +0200624 goto switchstate;
625 }
626
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100627 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200628 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100629 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +0100630 prev_state = appctx->st0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200631 reql = co_getline(si_oc(si), trash.area,
632 trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200633 if (reql <= 0) { /* closed or EOL not found */
634 if (reql == 0)
635 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100636 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200637 goto switchstate;
638 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200639 if (trash.area[reql-1] != '\n') {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100640 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200641 goto switchstate;
642 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200643 else if (reql > 1 && (trash.area[reql-2] == '\r'))
644 trash.area[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200645 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200646 trash.area[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200647
Willy Tarreau06d80a92017-10-19 14:32:15 +0200648 co_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200649
650 /* test hostname match */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200651 if (strcmp(localpeer, trash.area) != 0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100652 appctx->st0 = PEER_SESS_ST_EXIT;
653 appctx->st1 = PEER_SESS_SC_ERRHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +0200654 goto switchstate;
655 }
656
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100657 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200658 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100659 case PEER_SESS_ST_GETPEER: {
Emeric Brun2b920a12010-09-23 18:30:22 +0200660 char *p;
Willy Tarreau2d372c22018-11-05 17:12:27 +0100661
662 prev_state = appctx->st0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200663 reql = co_getline(si_oc(si), trash.area,
664 trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200665 if (reql <= 0) { /* closed or EOL not found */
666 if (reql == 0)
667 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100668 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200669 goto switchstate;
670 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200671 if (trash.area[reql-1] != '\n') {
Emeric Brun2b920a12010-09-23 18:30:22 +0200672 /* Incomplete line, we quit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100673 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200674 goto switchstate;
675 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200676 else if (reql > 1 && (trash.area[reql-2] == '\r'))
677 trash.area[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200678 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200679 trash.area[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200680
Willy Tarreau06d80a92017-10-19 14:32:15 +0200681 co_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200682
Emeric Brunb3971ab2015-05-12 18:49:09 +0200683 /* parse line "<peer name> <pid> <relative_pid>" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200684 p = strchr(trash.area, ' ');
Emeric Brun2b920a12010-09-23 18:30:22 +0200685 if (!p) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100686 appctx->st0 = PEER_SESS_ST_EXIT;
687 appctx->st1 = PEER_SESS_SC_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +0200688 goto switchstate;
689 }
690 *p = 0;
691
692 /* lookup known peer */
693 for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200694 if (strcmp(curpeer->id, trash.area) == 0)
Emeric Brun2b920a12010-09-23 18:30:22 +0200695 break;
696 }
697
698 /* if unknown peer */
699 if (!curpeer) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100700 appctx->st0 = PEER_SESS_ST_EXIT;
701 appctx->st1 = PEER_SESS_SC_ERRPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +0200702 goto switchstate;
703 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200704
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100705 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +0100706 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200707 if (curpeer->local) {
708 /* Local connection, reply a retry */
709 appctx->st0 = PEER_SESS_ST_EXIT;
710 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
711 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +0200712 }
Emeric Brun80527f52017-06-19 17:46:37 +0200713
714 /* we're killing a connection, we must apply a random delay before
715 * retrying otherwise the other end will do the same and we can loop
716 * for a while.
717 */
718 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100719 peer_session_forceshutdown(curpeer->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200720 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200721 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
722 if (min_ver == PEER_DWNGRD_MINOR_VER) {
723 curpeer->flags |= PEER_F_DWNGRD;
724 }
725 else {
726 curpeer->flags &= ~PEER_F_DWNGRD;
727 }
728 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200729 curpeer->appctx = appctx;
730 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100731 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Willy Tarreau199ad242018-11-05 16:31:22 +0100732 HA_ATOMIC_ADD(&active_peers, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +0200733 /* fall through */
734 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100735 case PEER_SESS_ST_SENDSUCCESS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200736 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200737
Willy Tarreau2d372c22018-11-05 17:12:27 +0100738 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +0200739 if (!curpeer) {
740 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100741 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200742 if (curpeer->appctx != appctx) {
743 appctx->st0 = PEER_SESS_ST_END;
744 goto switchstate;
745 }
746 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200747 repl = snprintf(trash.area, trash.size,
748 "%d\n",
749 PEER_SESS_SC_SUCCESSCODE);
750 repl = ci_putblk(si_ic(si), trash.area, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200751 if (repl <= 0) {
752 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100753 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100754 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200755 goto switchstate;
756 }
757
758 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200759 curpeer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200760
761 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200762 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200763
764 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200765 curpeer->confirm = 0;
766
767 /* Init cursors */
768 for (st = curpeer->tables; st ; st = st->next) {
769 st->last_get = st->last_acked = 0;
770 st->teaching_origin = st->last_pushed = st->update;
771 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200772
773 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200774 curpeer->flags &= PEER_TEACH_RESET;
775 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200776
777 /* if current peer is local */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200778 if (curpeer->local) {
779 /* if current host need resyncfrom local and no process assined */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200780 if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
781 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200782 /* assign local peer for a lesson, consider lesson already requested */
783 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200784 curpeers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200785 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200786
Emeric Brunb3971ab2015-05-12 18:49:09 +0200787 }
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200788 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
789 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200790 /* assign peer for a lesson */
791 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200792 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200793 }
794
795
Emeric Brun2b920a12010-09-23 18:30:22 +0200796 /* switch to waiting message state */
Willy Tarreau2d372c22018-11-05 17:12:27 +0100797 HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100798 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200799 goto switchstate;
800 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100801 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +0100802 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +0200803 if (!curpeer) {
804 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100805 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200806 if (curpeer->appctx != appctx) {
807 appctx->st0 = PEER_SESS_ST_END;
808 goto switchstate;
809 }
810 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200811
812 /* Send headers */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200813 repl = snprintf(trash.area, trash.size,
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200814 PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
815 PEER_MAJOR_VER,
816 (curpeer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200817 curpeer->id,
Emeric Brun2b920a12010-09-23 18:30:22 +0200818 localpeer,
Willy Tarreau7b77c9f2012-01-07 22:52:12 +0100819 (int)getpid(),
Emeric Brunb3971ab2015-05-12 18:49:09 +0200820 relative_pid);
Emeric Brun2b920a12010-09-23 18:30:22 +0200821
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100822 if (repl >= trash.size) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100823 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200824 goto switchstate;
825 }
826
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200827 repl = ci_putblk(si_ic(si), trash.area, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +0200828 if (repl <= 0) {
829 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +0100830 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100831 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200832 goto switchstate;
833 }
834
835 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100836 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200837 /* fall through */
838 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100839 case PEER_SESS_ST_GETSTATUS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200840 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +0200841
Willy Tarreau2d372c22018-11-05 17:12:27 +0100842 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +0200843 if (!curpeer) {
844 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100845 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200846 if (curpeer->appctx != appctx) {
847 appctx->st0 = PEER_SESS_ST_END;
848 goto switchstate;
849 }
850 }
851
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100852 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200853 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +0200854
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200855 reql = co_getline(si_oc(si), trash.area,
856 trash.size);
Emeric Brun2b920a12010-09-23 18:30:22 +0200857 if (reql <= 0) { /* closed or EOL not found */
858 if (reql == 0)
859 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100860 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200861 goto switchstate;
862 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200863 if (trash.area[reql-1] != '\n') {
Emeric Brun2b920a12010-09-23 18:30:22 +0200864 /* Incomplete line, we quit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100865 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200866 goto switchstate;
867 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200868 else if (reql > 1 && (trash.area[reql-2] == '\r'))
869 trash.area[reql-2] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200870 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200871 trash.area[reql-1] = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200872
Willy Tarreau06d80a92017-10-19 14:32:15 +0200873 co_skip(si_oc(si), reql);
Emeric Brun2b920a12010-09-23 18:30:22 +0200874
875 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200876 curpeer->statuscode = atoi(trash.area);
Emeric Brun2b920a12010-09-23 18:30:22 +0200877
878 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200879 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200880
881 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200882 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200883 /* Init cursors */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200884 for (st = curpeer->tables; st ; st = st->next) {
885 st->last_get = st->last_acked = 0;
886 st->teaching_origin = st->last_pushed = st->update;
887 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200888
889 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200890 curpeer->confirm = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +0200891
Emeric Brunb3971ab2015-05-12 18:49:09 +0200892 /* reset teaching and learning flags to 0 */
893 curpeer->flags &= PEER_TEACH_RESET;
894 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200895
Emeric Brunb3971ab2015-05-12 18:49:09 +0200896 /* If current peer is local */
897 if (curpeer->local) {
898 /* flag to start to teach lesson */
899 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +0200900
Emeric Brunb3971ab2015-05-12 18:49:09 +0200901 }
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200902 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
903 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200904 /* If peer is remote and resync from remote is needed,
905 and no peer currently assigned */
Emeric Brun2b920a12010-09-23 18:30:22 +0200906
Emeric Brunb3971ab2015-05-12 18:49:09 +0200907 /* assign peer for a lesson */
908 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200909 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +0200910 }
911
912 }
913 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200914 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
915 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +0200916 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100917 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +0200918 goto switchstate;
919 }
Willy Tarreau2d372c22018-11-05 17:12:27 +0100920 HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100921 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +0200922 /* fall through */
923 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100924 case PEER_SESS_ST_WAITMSG: {
Willy Tarreau9d9179b2013-04-11 16:56:44 +0200925 struct stksess *ts, *newts = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200926 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200927 char *msg_cur = trash.area;
928 char *msg_end = trash.area;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200929 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +0200930 int totl = 0;
931
Willy Tarreau2d372c22018-11-05 17:12:27 +0100932 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +0200933 if (!curpeer) {
934 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100935 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +0200936 if (curpeer->appctx != appctx) {
937 appctx->st0 = PEER_SESS_ST_END;
938 goto switchstate;
939 }
940 }
941
Willy Tarreau06d80a92017-10-19 14:32:15 +0200942 reql = co_getblk(si_oc(si), (char *)msg_head, 2*sizeof(unsigned char), totl);
Willy Tarreau72d6c162013-04-11 16:14:13 +0200943 if (reql <= 0) /* closed or EOL not found */
944 goto incomplete;
945
Emeric Brun2b920a12010-09-23 18:30:22 +0200946 totl += reql;
947
Emeric Brunb3971ab2015-05-12 18:49:09 +0200948 if (msg_head[1] >= 128) {
949 /* Read and Decode message length */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200950 reql = co_getblk(si_oc(si), (char *)&msg_head[2], sizeof(unsigned char), totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200951 if (reql <= 0) /* closed */
952 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +0200953
Emeric Brunb3971ab2015-05-12 18:49:09 +0200954 totl += reql;
955
956 if (msg_head[2] < 240) {
957 msg_len = msg_head[2];
Emeric Brun2b920a12010-09-23 18:30:22 +0200958 }
959 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200960 int i;
961 char *cur;
962 char *end;
Willy Tarreau72d6c162013-04-11 16:14:13 +0200963
Emeric Brunb3971ab2015-05-12 18:49:09 +0200964 for (i = 3 ; i < sizeof(msg_head) ; i++) {
Willy Tarreau06d80a92017-10-19 14:32:15 +0200965 reql = co_getblk(si_oc(si), (char *)&msg_head[i], sizeof(char), totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200966 if (reql <= 0) /* closed */
967 goto incomplete;
968
969 totl += reql;
970
971 if (!(msg_head[i] & 0x80))
972 break;
973 }
974
975 if (i == sizeof(msg_head)) {
976 /* malformed message */
977 appctx->st0 = PEER_SESS_ST_ERRPROTO;
978 goto switchstate;
979
980 }
981 end = (char *)msg_head + sizeof(msg_head);
982 cur = (char *)&msg_head[2];
983 msg_len = intdecode(&cur, end);
984 if (!cur) {
985 /* malformed message */
986 appctx->st0 = PEER_SESS_ST_ERRPROTO;
987 goto switchstate;
988 }
Emeric Brun2b920a12010-09-23 18:30:22 +0200989 }
990
Willy Tarreau86a446e2013-11-25 23:02:37 +0100991
Emeric Brunb3971ab2015-05-12 18:49:09 +0200992 /* Read message content */
993 if (msg_len) {
994 if (msg_len > trash.size) {
995 /* Status code is not success, abort */
996 appctx->st0 = PEER_SESS_ST_ERRSIZE;
997 goto switchstate;
998 }
Willy Tarreau72d6c162013-04-11 16:14:13 +0200999
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001000 reql = co_getblk(si_oc(si),
1001 trash.area,
1002 msg_len,
1003 totl);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001004 if (reql <= 0) /* closed */
1005 goto incomplete;
Emeric Brun2b920a12010-09-23 18:30:22 +02001006 totl += reql;
Willy Tarreau86a446e2013-11-25 23:02:37 +01001007
Emeric Brunb3971ab2015-05-12 18:49:09 +02001008 msg_end += msg_len;
1009 }
1010 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01001011
Emeric Brunb3971ab2015-05-12 18:49:09 +02001012 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
1013 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
1014 struct shared_table *st;
1015 /* Reset message: remote need resync */
1016
1017 /* prepare tables fot a global push */
1018 for (st = curpeer->tables; st; st = st->next) {
1019 st->teaching_origin = st->last_pushed = st->table->update;
1020 st->flags = 0;
Willy Tarreau86a446e2013-11-25 23:02:37 +01001021 }
Willy Tarreau72d6c162013-04-11 16:14:13 +02001022
Emeric Brunb3971ab2015-05-12 18:49:09 +02001023 /* reset teaching flags to 0 */
1024 curpeer->flags &= PEER_TEACH_RESET;
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001025
Emeric Brunb3971ab2015-05-12 18:49:09 +02001026 /* flag to start to teach lesson */
1027 curpeer->flags |= PEER_F_TEACH_PROCESS;
1028
1029
1030 }
1031 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
1032
1033 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
1034 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001035 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1036 curpeers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Willy Tarreau86a446e2013-11-25 23:02:37 +01001037 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001038 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001039 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001040 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
1041
1042 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
1043 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001044 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001045
1046 curpeer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001047 curpeers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
1048 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Cyril Bonté9a60ff92014-02-16 01:07:07 +01001049 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001050 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001051 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001052 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
Emeric Brun597b26e2016-08-12 11:23:31 +02001053 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001054
1055 /* If stopping state */
1056 if (stopping) {
1057 /* Close session, push resync no more needed */
1058 curpeer->flags |= PEER_F_TEACH_COMPLETE;
1059 appctx->st0 = PEER_SESS_ST_END;
1060 goto switchstate;
1061 }
Emeric Brun597b26e2016-08-12 11:23:31 +02001062 for (st = curpeer->tables; st; st = st->next) {
1063 st->update = st->last_pushed = st->teaching_origin;
1064 st->flags = 0;
1065 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001066
1067 /* reset teaching flags to 0 */
1068 curpeer->flags &= PEER_TEACH_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001069 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001070 }
1071 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1072 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
1073 int table_id_len;
1074 struct shared_table *st;
1075 int table_type;
1076 int table_keylen;
1077 int table_id;
1078 uint64_t table_data;
Emeric Brun2b920a12010-09-23 18:30:22 +02001079
Emeric Brunb3971ab2015-05-12 18:49:09 +02001080 table_id = intdecode(&msg_cur, msg_end);
1081 if (!msg_cur) {
1082 /* malformed message */
1083 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1084 goto switchstate;
1085 }
Willy Tarreau72d6c162013-04-11 16:14:13 +02001086
Emeric Brunb3971ab2015-05-12 18:49:09 +02001087 table_id_len = intdecode(&msg_cur, msg_end);
1088 if (!msg_cur) {
1089 /* malformed message */
1090 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1091 goto switchstate;
1092 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001093
Emeric Brunb3971ab2015-05-12 18:49:09 +02001094 curpeer->remote_table = NULL;
1095 if (!table_id_len || (msg_cur + table_id_len) >= msg_end) {
1096 /* malformed message */
1097 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1098 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001099 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001100
Emeric Brunb3971ab2015-05-12 18:49:09 +02001101 for (st = curpeer->tables; st; st = st->next) {
1102 /* Reset IDs */
1103 if (st->remote_id == table_id)
1104 st->remote_id = 0;
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001105
Emeric Brunb3971ab2015-05-12 18:49:09 +02001106 if (!curpeer->remote_table
1107 && (table_id_len == strlen(st->table->id))
1108 && (memcmp(st->table->id, msg_cur, table_id_len) == 0)) {
1109 curpeer->remote_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001110 }
1111 }
1112
Emeric Brunb3971ab2015-05-12 18:49:09 +02001113 if (!curpeer->remote_table) {
1114 goto ignore_msg;
1115 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001116
Emeric Brunb3971ab2015-05-12 18:49:09 +02001117 msg_cur += table_id_len;
1118 if (msg_cur >= msg_end) {
1119 /* malformed message */
1120 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1121 goto switchstate;
1122 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001123
Emeric Brunb3971ab2015-05-12 18:49:09 +02001124 table_type = intdecode(&msg_cur, msg_end);
1125 if (!msg_cur) {
1126 /* malformed message */
1127 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1128 goto switchstate;
1129 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001130
Emeric Brunb3971ab2015-05-12 18:49:09 +02001131 table_keylen = intdecode(&msg_cur, msg_end);
1132 if (!msg_cur) {
1133 /* malformed message */
1134 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1135 goto switchstate;
1136 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001137
Emeric Brunb3971ab2015-05-12 18:49:09 +02001138 table_data = intdecode(&msg_cur, msg_end);
1139 if (!msg_cur) {
1140 /* malformed message */
1141 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1142 goto switchstate;
1143 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001144
Emeric Brunb3971ab2015-05-12 18:49:09 +02001145 if (curpeer->remote_table->table->type != table_type
1146 || curpeer->remote_table->table->key_size != table_keylen) {
1147 curpeer->remote_table = NULL;
1148 goto ignore_msg;
1149 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001150
Emeric Brunb3971ab2015-05-12 18:49:09 +02001151 curpeer->remote_table->remote_data = table_data;
1152 curpeer->remote_table->remote_id = table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001153 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001154 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1155 struct shared_table *st;
1156 int table_id;
Emeric Brun2b920a12010-09-23 18:30:22 +02001157
Emeric Brunb3971ab2015-05-12 18:49:09 +02001158 table_id = intdecode(&msg_cur, msg_end);
1159 if (!msg_cur) {
1160 /* malformed message */
1161 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1162 goto switchstate;
1163 }
1164 curpeer->remote_table = NULL;
1165 for (st = curpeer->tables; st; st = st->next) {
1166 if (st->remote_id == table_id) {
1167 curpeer->remote_table = st;
1168 break;
1169 }
1170 }
1171
Emeric Brun2b920a12010-09-23 18:30:22 +02001172 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001173 else if (msg_head[1] == PEER_MSG_STKT_UPDATE
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001174 || msg_head[1] == PEER_MSG_STKT_INCUPDATE
1175 || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED
1176 || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001177 struct shared_table *st = curpeer->remote_table;
1178 uint32_t update;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001179 int expire;
Emeric Brun94900952015-06-11 18:25:54 +02001180 unsigned int data_type;
1181 void *data_ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +02001182
Emeric Brunb3971ab2015-05-12 18:49:09 +02001183 /* Here we have data message */
1184 if (!st)
1185 goto ignore_msg;
Emeric Brun2b920a12010-09-23 18:30:22 +02001186
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001187 expire = MS_TO_TICKS(st->table->expire);
1188
1189 if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1190 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001191 if (msg_len < sizeof(update)) {
1192 /* malformed message */
1193 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1194 goto switchstate;
1195 }
1196 memcpy(&update, msg_cur, sizeof(update));
1197 msg_cur += sizeof(update);
1198 st->last_get = htonl(update);
1199 }
1200 else {
1201 st->last_get++;
1202 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001203
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001204 if (msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1205 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1206 size_t expire_sz = sizeof expire;
1207
1208 if (msg_cur + expire_sz > msg_end) {
1209 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1210 goto switchstate;
1211 }
1212 memcpy(&expire, msg_cur, expire_sz);
1213 msg_cur += expire_sz;
1214 expire = ntohl(expire);
1215 }
1216
Emeric Brunb3971ab2015-05-12 18:49:09 +02001217 newts = stksess_new(st->table, NULL);
1218 if (!newts)
1219 goto ignore_msg;
Emeric Brun55482912018-01-22 15:10:08 +01001220
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001221 if (st->table->type == SMP_T_STR) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001222 unsigned int to_read, to_store;
Emeric Brun2b920a12010-09-23 18:30:22 +02001223
Emeric Brunb3971ab2015-05-12 18:49:09 +02001224 to_read = intdecode(&msg_cur, msg_end);
1225 if (!msg_cur) {
1226 /* malformed message */
1227 stksess_free(st->table, newts);
1228 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1229 goto switchstate;
1230 }
1231 to_store = MIN(to_read, st->table->key_size - 1);
1232 if (msg_cur + to_store > msg_end) {
1233 /* malformed message */
1234 stksess_free(st->table, newts);
1235 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1236 goto switchstate;
1237 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001238
Emeric Brunb3971ab2015-05-12 18:49:09 +02001239 memcpy(newts->key.key, msg_cur, to_store);
1240 newts->key.key[to_store] = 0;
1241 msg_cur += to_read;
1242 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001243 else if (st->table->type == SMP_T_SINT) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001244 unsigned int netinteger;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001245
Emeric Brunb3971ab2015-05-12 18:49:09 +02001246 if (msg_cur + sizeof(netinteger) > msg_end) {
1247 /* malformed message */
1248 stksess_free(st->table, newts);
1249 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1250 goto switchstate;
1251 }
1252 memcpy(&netinteger, msg_cur, sizeof(netinteger));
1253 netinteger = ntohl(netinteger);
1254 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1255 msg_cur += sizeof(netinteger);
1256 }
1257 else {
1258 if (msg_cur + st->table->key_size > msg_end) {
1259 /* malformed message */
1260 stksess_free(st->table, newts);
1261 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1262 goto switchstate;
1263 }
1264 memcpy(newts->key.key, msg_cur, st->table->key_size);
1265 msg_cur += st->table->key_size;
1266 }
1267
1268 /* lookup for existing entry */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001269 ts = stktable_set_entry(st->table, newts);
1270 if (ts != newts) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001271 stksess_free(st->table, newts);
1272 newts = NULL;
1273 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001274
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001275 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001276
Emeric Brun94900952015-06-11 18:25:54 +02001277 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001278
Emeric Brun94900952015-06-11 18:25:54 +02001279 if ((1 << data_type) & st->remote_data) {
1280 switch (stktable_data_types[data_type].std_type) {
1281 case STD_T_SINT: {
1282 int data;
1283
1284 data = intdecode(&msg_cur, msg_end);
1285 if (!msg_cur) {
1286 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001287 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001288 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001289 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1290 goto switchstate;
1291 }
1292
1293 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1294 if (data_ptr)
1295 stktable_data_cast(data_ptr, std_t_sint) = data;
1296 break;
1297 }
1298 case STD_T_UINT: {
1299 unsigned int data;
1300
1301 data = intdecode(&msg_cur, msg_end);
1302 if (!msg_cur) {
1303 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001304 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001305 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001306 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1307 goto switchstate;
1308 }
1309
1310 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1311 if (data_ptr)
1312 stktable_data_cast(data_ptr, std_t_uint) = data;
1313 break;
1314 }
1315 case STD_T_ULL: {
1316 unsigned long long data;
1317
1318 data = intdecode(&msg_cur, msg_end);
1319 if (!msg_cur) {
1320 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001321 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001322 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001323 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1324 goto switchstate;
1325 }
1326
1327 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1328 if (data_ptr)
1329 stktable_data_cast(data_ptr, std_t_ull) = data;
1330 break;
1331 }
1332 case STD_T_FRQP: {
1333 struct freq_ctr_period data;
1334
Emeric Brunf2fc1fd2017-11-02 17:32:43 +01001335 /* First bit is reserved for the freq_ctr_period lock
1336 Note: here we're still protected by the stksess lock
1337 so we don't need to update the update the freq_ctr_period
1338 using its internal lock */
1339
1340 data.curr_tick = tick_add(now_ms, -intdecode(&msg_cur, msg_end)) & ~0x1;
Emeric Brun94900952015-06-11 18:25:54 +02001341 if (!msg_cur) {
1342 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001343 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001344 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001345 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1346 goto switchstate;
1347 }
1348 data.curr_ctr = intdecode(&msg_cur, msg_end);
1349 if (!msg_cur) {
1350 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001351 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001352 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001353 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1354 goto switchstate;
1355 }
1356 data.prev_ctr = intdecode(&msg_cur, msg_end);
1357 if (!msg_cur) {
1358 /* malformed message */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001359 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001360 stktable_touch_remote(st->table, ts, 1);
Emeric Brun94900952015-06-11 18:25:54 +02001361 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1362 goto switchstate;
1363 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001364
Emeric Brun94900952015-06-11 18:25:54 +02001365 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1366 if (data_ptr)
1367 stktable_data_cast(data_ptr, std_t_frqp) = data;
1368 break;
1369 }
1370 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001371 }
1372 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001373
Emeric Brun55482912018-01-22 15:10:08 +01001374 /* Force new expiration */
1375 ts->expire = tick_add(now_ms, expire);
1376
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001377 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001378 stktable_touch_remote(st->table, ts, 1);
1379
Emeric Brunb3971ab2015-05-12 18:49:09 +02001380 }
1381 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1382 /* ack message */
1383 uint32_t table_id ;
1384 uint32_t update;
1385 struct shared_table *st;
1386
1387 table_id = intdecode(&msg_cur, msg_end);
1388 if (!msg_cur || (msg_cur + sizeof(update) > msg_end)) {
1389 /* malformed message */
1390 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1391 goto switchstate;
1392 }
1393 memcpy(&update, msg_cur, sizeof(update));
1394 update = ntohl(update);
Emeric Brun2b920a12010-09-23 18:30:22 +02001395
Emeric Brunb3971ab2015-05-12 18:49:09 +02001396 for (st = curpeer->tables; st; st = st->next) {
1397 if (st->local_id == table_id) {
1398 st->update = update;
1399 break;
1400 }
1401 }
1402 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001403 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001404 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1405 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +02001406 goto switchstate;
1407 }
1408
Emeric Brunb3971ab2015-05-12 18:49:09 +02001409ignore_msg:
Emeric Brun2b920a12010-09-23 18:30:22 +02001410 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001411 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02001412 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02001413 goto switchstate;
1414
Emeric Brun2b920a12010-09-23 18:30:22 +02001415incomplete:
Willy Tarreau06d80a92017-10-19 14:32:15 +02001416 /* we get here when a co_getblk() returns <= 0 in reql */
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001417
Willy Tarreau72d6c162013-04-11 16:14:13 +02001418 if (reql < 0) {
1419 /* there was an error */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001420 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001421 goto switchstate;
1422 }
1423
Emeric Brun2b920a12010-09-23 18:30:22 +02001424
Emeric Brun2b920a12010-09-23 18:30:22 +02001425
Emeric Brunb3971ab2015-05-12 18:49:09 +02001426
Emeric Brun2b920a12010-09-23 18:30:22 +02001427 /* Need to request a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001428 if ((curpeer->flags & PEER_F_LEARN_ASSIGN) &&
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001429 (curpeers->flags & PEERS_F_RESYNC_ASSIGN) &&
1430 !(curpeers->flags & PEERS_F_RESYNC_PROCESS)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001431 unsigned char msg[2];
Emeric Brun2b920a12010-09-23 18:30:22 +02001432
Emeric Brunb3971ab2015-05-12 18:49:09 +02001433 /* Current peer was elected to request a resync */
1434 msg[0] = PEER_MSG_CLASS_CONTROL;
1435 msg[1] = PEER_MSG_CTRL_RESYNCREQ;
Emeric Brun2b920a12010-09-23 18:30:22 +02001436
Emeric Brunb3971ab2015-05-12 18:49:09 +02001437 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001438 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001439 if (repl <= 0) {
1440 /* no more write possible */
1441 if (repl == -1)
1442 goto full;
1443 appctx->st0 = PEER_SESS_ST_END;
1444 goto switchstate;
1445 }
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001446 curpeers->flags |= PEERS_F_RESYNC_PROCESS;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001447 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001448
Emeric Brunb3971ab2015-05-12 18:49:09 +02001449 /* Nothing to read, now we start to write */
Emeric Brun2b920a12010-09-23 18:30:22 +02001450
Emeric Brunb3971ab2015-05-12 18:49:09 +02001451 if (curpeer->tables) {
1452 struct shared_table *st;
1453 struct shared_table *last_local_table;
Emeric Brun2b920a12010-09-23 18:30:22 +02001454
Emeric Brunb3971ab2015-05-12 18:49:09 +02001455 last_local_table = curpeer->last_local_table;
1456 if (!last_local_table)
1457 last_local_table = curpeer->tables;
1458 st = last_local_table->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001459
Emeric Brunb3971ab2015-05-12 18:49:09 +02001460 while (1) {
1461 if (!st)
1462 st = curpeer->tables;
Emeric Brun2b920a12010-09-23 18:30:22 +02001463
Emeric Brunb3971ab2015-05-12 18:49:09 +02001464 /* It remains some updates to ack */
1465 if (st->last_get != st->last_acked) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001466 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001467
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001468 msglen = peer_prepare_ackmsg(st,
1469 trash.area,
1470 trash.size);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001471 if (!msglen) {
1472 /* internal error: message does not fit in trash */
1473 appctx->st0 = PEER_SESS_ST_END;
1474 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001475 }
1476
Emeric Brunb3971ab2015-05-12 18:49:09 +02001477 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001478 repl = ci_putblk(si_ic(si),
1479 trash.area,
1480 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001481 if (repl <= 0) {
1482 /* no more write possible */
1483 if (repl == -1) {
1484 goto full;
Emeric Brun2b920a12010-09-23 18:30:22 +02001485 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001486 appctx->st0 = PEER_SESS_ST_END;
1487 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001488 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001489 st->last_acked = st->last_get;
Emeric Brun2b920a12010-09-23 18:30:22 +02001490 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001491
Emeric Brunb3971ab2015-05-12 18:49:09 +02001492 if (!(curpeer->flags & PEER_F_TEACH_PROCESS)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001493 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001494 if (!(curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1495 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1496 struct eb32_node *eb;
1497 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001498
Emeric Brunb3971ab2015-05-12 18:49:09 +02001499 if (st != curpeer->last_local_table) {
1500 int msglen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001501
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001502 msglen = peer_prepare_switchmsg(st,
1503 trash.area,
1504 trash.size);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001505 if (!msglen) {
Emeric Brun088c9b72017-12-01 11:37:36 +01001506 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001507 /* internal error: message does not fit in trash */
1508 appctx->st0 = PEER_SESS_ST_END;
1509 goto switchstate;
1510 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001511
Emeric Brunb3971ab2015-05-12 18:49:09 +02001512 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001513 repl = ci_putblk(si_ic(si),
1514 trash.area,
1515 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001516 if (repl <= 0) {
Emeric Brun088c9b72017-12-01 11:37:36 +01001517 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001518 /* no more write possible */
1519 if (repl == -1) {
1520 goto full;
1521 }
1522 appctx->st0 = PEER_SESS_ST_END;
1523 goto switchstate;
1524 }
1525 curpeer->last_local_table = st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001526 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001527
1528 /* We force new pushed to 1 to force identifier in update message */
1529 new_pushed = 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001530 while (1) {
1531 uint32_t msglen;
1532 struct stksess *ts;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001533 unsigned updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001534
1535 /* push local updates */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001536 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001537 if (!eb) {
1538 eb = eb32_first(&st->table->updates);
1539 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001540 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001541 break;
1542 }
1543 }
1544
1545 if ((int)(eb->key - st->table->localupdate) > 0) {
Emeric Brunaaf58602015-06-15 17:23:30 +02001546 st->table->commitupdate = st->last_pushed = st->table->localupdate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001547 break;
1548 }
1549
1550 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001551 updateid = ts->upd.key;
1552 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001553 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001554
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001555 msglen = peer_prepare_updatemsg(ts, st, updateid,
1556 trash.area,
1557 trash.size,
1558 new_pushed,
1559 0);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001560 if (!msglen) {
1561 /* internal error: message does not fit in trash */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001562 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001563 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001564 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001565 appctx->st0 = PEER_SESS_ST_END;
1566 goto switchstate;
1567 }
1568
1569 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001570 repl = ci_putblk(si_ic(si),
1571 trash.area,
1572 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001573 if (repl <= 0) {
1574 /* no more write possible */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001575 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001576 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001577 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001578 if (repl == -1) {
1579 goto full;
1580 }
1581 appctx->st0 = PEER_SESS_ST_END;
1582 goto switchstate;
1583 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001584
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001585 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001586 ts->ref_cnt--;
1587 st->last_pushed = updateid;
Emeric Brunaaf58602015-06-15 17:23:30 +02001588 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
1589 st->table->commitupdate = st->last_pushed;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001590 /* identifier may not needed in next update message */
1591 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001592 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001593 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001594 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001595 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001596 else {
1597 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1598 struct eb32_node *eb;
1599 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001600
Emeric Brunb3971ab2015-05-12 18:49:09 +02001601 if (st != curpeer->last_local_table) {
1602 int msglen;
1603
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001604 msglen = peer_prepare_switchmsg(st,
1605 trash.area,
1606 trash.size);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001607 if (!msglen) {
1608 /* internal error: message does not fit in trash */
1609 appctx->st0 = PEER_SESS_ST_END;
1610 goto switchstate;
1611 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001612
Emeric Brunb3971ab2015-05-12 18:49:09 +02001613 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001614 repl = ci_putblk(si_ic(si),
1615 trash.area,
1616 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001617 if (repl <= 0) {
1618 /* no more write possible */
1619 if (repl == -1) {
1620 goto full;
1621 }
1622 appctx->st0 = PEER_SESS_ST_END;
1623 goto switchstate;
1624 }
1625 curpeer->last_local_table = st;
1626 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001627
Emeric Brunb3971ab2015-05-12 18:49:09 +02001628 /* We force new pushed to 1 to force identifier in update message */
1629 new_pushed = 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001630 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001631 while (1) {
1632 uint32_t msglen;
1633 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001634 int use_timed;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001635 unsigned updateid;
Emeric Brun2b920a12010-09-23 18:30:22 +02001636
Emeric Brunb3971ab2015-05-12 18:49:09 +02001637 /* push local updates */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001638 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001639 if (!eb) {
1640 st->flags |= SHTABLE_F_TEACH_STAGE1;
1641 eb = eb32_first(&st->table->updates);
1642 if (eb)
1643 st->last_pushed = eb->key - 1;
1644 break;
1645 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001646
Emeric Brunb3971ab2015-05-12 18:49:09 +02001647 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001648 updateid = ts->upd.key;
1649 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001650 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001651
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001652 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001653 msglen = peer_prepare_updatemsg(ts, st, updateid,
1654 trash.area,
1655 trash.size,
1656 new_pushed,
1657 use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001658 if (!msglen) {
1659 /* internal error: message does not fit in trash */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001660 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001661 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001662 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001663 appctx->st0 = PEER_SESS_ST_END;
1664 goto switchstate;
1665 }
1666
1667 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001668 repl = ci_putblk(si_ic(si),
1669 trash.area,
1670 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001671 if (repl <= 0) {
1672 /* no more write possible */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001673 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001674 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001675 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001676 if (repl == -1) {
1677 goto full;
1678 }
1679 appctx->st0 = PEER_SESS_ST_END;
1680 goto switchstate;
1681 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001682 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001683 ts->ref_cnt--;
1684 st->last_pushed = updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001685 /* identifier may not needed in next update message */
1686 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001687 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001688 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001689 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001690
Emeric Brunb3971ab2015-05-12 18:49:09 +02001691 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1692 struct eb32_node *eb;
1693 int new_pushed;
Emeric Brun2b920a12010-09-23 18:30:22 +02001694
Emeric Brunb3971ab2015-05-12 18:49:09 +02001695 if (st != curpeer->last_local_table) {
1696 int msglen;
1697
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001698 msglen = peer_prepare_switchmsg(st,
1699 trash.area,
1700 trash.size);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001701 if (!msglen) {
1702 /* internal error: message does not fit in trash */
1703 appctx->st0 = PEER_SESS_ST_END;
1704 goto switchstate;
1705 }
1706
1707 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001708 repl = ci_putblk(si_ic(si),
1709 trash.area,
1710 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001711 if (repl <= 0) {
1712 /* no more write possible */
1713 if (repl == -1) {
1714 goto full;
1715 }
1716 appctx->st0 = PEER_SESS_ST_END;
1717 goto switchstate;
1718 }
1719 curpeer->last_local_table = st;
1720 }
1721
1722 /* We force new pushed to 1 to force identifier in update message */
1723 new_pushed = 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001724 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001725 while (1) {
1726 uint32_t msglen;
1727 struct stksess *ts;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001728 int use_timed;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001729 unsigned updateid;
1730
1731 /* push local updates */
1732 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001733
1734 /* push local updates */
1735 if (!eb || eb->key > st->teaching_origin) {
1736 st->flags |= SHTABLE_F_TEACH_STAGE2;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001737 break;
1738 }
1739
1740 ts = eb32_entry(eb, struct stksess, upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001741 updateid = ts->upd.key;
1742 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001743 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001744
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001745 use_timed = !(curpeer->flags & PEER_F_DWNGRD);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001746 msglen = peer_prepare_updatemsg(ts, st, updateid,
1747 trash.area,
1748 trash.size,
1749 new_pushed,
1750 use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001751 if (!msglen) {
1752 /* internal error: message does not fit in trash */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001753 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001754 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001755 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001756 appctx->st0 = PEER_SESS_ST_END;
1757 goto switchstate;
1758 }
1759
1760 /* message to buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001761 repl = ci_putblk(si_ic(si),
1762 trash.area,
1763 msglen);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001764 if (repl <= 0) {
1765 /* no more write possible */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001766 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001767 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001768 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001769 if (repl == -1) {
1770 goto full;
1771 }
1772 appctx->st0 = PEER_SESS_ST_END;
1773 goto switchstate;
1774 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001775
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001776 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001777 ts->ref_cnt--;
1778 st->last_pushed = updateid;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001779 /* identifier may not needed in next update message */
1780 new_pushed = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001781 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001782 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001783 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001784 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001785
1786 if (st == last_local_table)
1787 break;
1788 st = st->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001789 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001790 }
1791
1792
1793 if ((curpeer->flags & PEER_F_TEACH_PROCESS) && !(curpeer->flags & PEER_F_TEACH_FINISHED)) {
1794 unsigned char msg[2];
1795
1796 /* Current peer was elected to request a resync */
1797 msg[0] = PEER_MSG_CLASS_CONTROL;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001798 msg[1] = ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED) ? PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001799 /* process final lesson message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001800 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001801 if (repl <= 0) {
1802 /* no more write possible */
1803 if (repl == -1)
1804 goto full;
1805 appctx->st0 = PEER_SESS_ST_END;
1806 goto switchstate;
1807 }
1808 /* flag finished message sent */
1809 curpeer->flags |= PEER_F_TEACH_FINISHED;
1810 }
1811
Emeric Brun597b26e2016-08-12 11:23:31 +02001812 /* Confirm finished or partial messages */
1813 while (curpeer->confirm) {
1814 unsigned char msg[2];
1815
1816 /* There is a confirm messages to send */
1817 msg[0] = PEER_MSG_CLASS_CONTROL;
1818 msg[1] = PEER_MSG_CTRL_RESYNCCONFIRM;
1819
1820 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001821 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brun597b26e2016-08-12 11:23:31 +02001822 if (repl <= 0) {
1823 /* no more write possible */
1824 if (repl == -1)
1825 goto full;
1826 appctx->st0 = PEER_SESS_ST_END;
1827 goto switchstate;
1828 }
1829 curpeer->confirm--;
1830 }
1831
Emeric Brun2b920a12010-09-23 18:30:22 +02001832 /* noting more to do */
1833 goto out;
1834 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001835 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001836 if (prev_state == PEER_SESS_ST_WAITMSG)
1837 HA_ATOMIC_SUB(&connected_peers, 1);
1838 prev_state = appctx->st0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001839 repl = snprintf(trash.area, trash.size,
1840 "%d\n", appctx->st1);
1841 if (ci_putblk(si_ic(si), trash.area, repl) == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001842 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001843 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001844 goto switchstate;
1845 case PEER_SESS_ST_ERRSIZE: {
1846 unsigned char msg[2];
1847
Willy Tarreau2d372c22018-11-05 17:12:27 +01001848 if (prev_state == PEER_SESS_ST_WAITMSG)
1849 HA_ATOMIC_SUB(&connected_peers, 1);
1850 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001851 msg[0] = PEER_MSG_CLASS_ERROR;
1852 msg[1] = PEER_MSG_ERR_SIZELIMIT;
1853
Willy Tarreau06d80a92017-10-19 14:32:15 +02001854 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001855 goto full;
1856 appctx->st0 = PEER_SESS_ST_END;
1857 goto switchstate;
1858 }
1859 case PEER_SESS_ST_ERRPROTO: {
1860 unsigned char msg[2];
1861
Willy Tarreau2d372c22018-11-05 17:12:27 +01001862 if (prev_state == PEER_SESS_ST_WAITMSG)
1863 HA_ATOMIC_SUB(&connected_peers, 1);
1864 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001865 msg[0] = PEER_MSG_CLASS_ERROR;
1866 msg[1] = PEER_MSG_ERR_PROTOCOL;
1867
Willy Tarreau06d80a92017-10-19 14:32:15 +02001868 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001869 goto full;
1870 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01001871 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001872 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001873 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001874 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01001875 if (prev_state == PEER_SESS_ST_WAITMSG)
1876 HA_ATOMIC_SUB(&connected_peers, 1);
1877 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02001878 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001879 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02001880 curpeer = NULL;
1881 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02001882 si_shutw(si);
1883 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001884 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02001885 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001886 }
1887 }
1888 }
1889out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001890 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02001891
1892 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001893 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001894 return;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001895full:
Willy Tarreaudb398432018-11-15 11:08:52 +01001896 si_rx_room_blk(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001897 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001898}
1899
Willy Tarreau30576452015-04-13 13:50:30 +02001900static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001901 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001902 .name = "<PEER>", /* used for logging */
1903 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07001904 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001905};
Emeric Brun2b920a12010-09-23 18:30:22 +02001906
1907/*
1908 * Use this function to force a close of a peer session
1909 */
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001910static void peer_session_forceshutdown(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001911{
Frédéric Lécaille5df11902017-06-13 16:39:57 +02001912 /* Note that the peer sessions which have just been created
1913 * (->st0 == PEER_SESS_ST_CONNECT) must not
1914 * be shutdown, if not, the TCP session will never be closed
1915 * and stay in CLOSE_WAIT state after having been closed by
1916 * the remote side.
1917 */
1918 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001919 return;
1920
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001921 if (appctx->applet != &peer_applet)
1922 return;
1923
Willy Tarreau2d372c22018-11-05 17:12:27 +01001924 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
1925 HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001926 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01001927 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001928}
1929
Willy Tarreau91d96282015-03-13 15:47:26 +01001930/* Pre-configures a peers frontend to accept incoming connections */
1931void peers_setup_frontend(struct proxy *fe)
1932{
1933 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001934 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau91d96282015-03-13 15:47:26 +01001935 fe->maxconn = 0;
1936 fe->conn_retries = CONN_RETRIES;
1937 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01001938 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01001939 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01001940 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02001941 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01001942}
1943
Emeric Brun2b920a12010-09-23 18:30:22 +02001944/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01001945 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02001946 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01001947static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02001948{
Willy Tarreau04b92862017-09-15 11:01:04 +02001949 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001950 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02001951 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02001952 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001953 struct connection *conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001954 struct conn_stream *cs;
Emeric Brun2b920a12010-09-23 18:30:22 +02001955
Emeric Brunb3971ab2015-05-12 18:49:09 +02001956 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
1957 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001958 s = NULL;
1959
Emeric Brun1138fd02017-06-19 12:38:55 +02001960 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02001961 if (!appctx)
1962 goto out_close;
1963
1964 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001965 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02001966
Willy Tarreau04b92862017-09-15 11:01:04 +02001967 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02001968 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001969 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02001970 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001971 }
1972
Willy Tarreau87787ac2017-08-28 16:22:54 +02001973 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001974 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02001975 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02001976 }
1977
Willy Tarreau342bfb12015-04-05 01:35:34 +02001978 /* The tasks below are normally what is supposed to be done by
1979 * fe->accept().
1980 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001981 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001982
Willy Tarreau6e2979c2015-04-27 13:21:15 +02001983 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001984 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02001985 appctx_wakeup(appctx);
1986
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001987 /* initiate an outgoing connection */
Willy Tarreaudbd02672017-12-06 17:39:53 +01001988 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001989 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02001990
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001991 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001992 * pre-initialized connection in si->conn.
1993 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001994 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02001995 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001996
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001997 if (unlikely((cs = cs_new(conn)) == NULL))
1998 goto out_free_conn;
1999
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002000 conn->target = s->target = peer_session_target(peer, s);
Willy Tarreaube373152018-09-06 11:45:30 +02002001 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
2002
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002003 conn_prepare(conn, peer->proto, peer_xprt(peer));
Olivier Houchardf502aca2018-12-14 19:42:40 +01002004 conn_install_mux(conn, &mux_pt_ops, cs, s->be, NULL);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002005 si_attach_cs(&s->si[1], cs);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002006
Emeric Brun2b920a12010-09-23 18:30:22 +02002007 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002008 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002009
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002010 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01002011
Emeric Brunb3971ab2015-05-12 18:49:09 +02002012 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02002013 task_wakeup(s->task, TASK_WOKEN_INIT);
Willy Tarreau199ad242018-11-05 16:31:22 +01002014 HA_ATOMIC_ADD(&active_peers, 1);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002015 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002016
2017 /* Error unrolling */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002018 out_free_conn:
2019 conn_free(conn);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002020 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02002021 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002022 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002023 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002024 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002025 out_free_appctx:
2026 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002027 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002028 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002029}
2030
2031/*
2032 * Task processing function to manage re-connect and peer session
2033 * tasks wakeup on local update.
2034 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002035static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002036{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002037 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002038 struct peer *ps;
2039 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002040
2041 task->expire = TICK_ETERNITY;
2042
Emeric Brunb3971ab2015-05-12 18:49:09 +02002043 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002044 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002045 signal_unregister_handler(peers->sighandler);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002046 task_delete(peers->sync_task);
2047 task_free(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002048 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002049 return NULL;
2050 }
2051
Emeric Brun80527f52017-06-19 17:46:37 +02002052 /* Acquire lock for all peers of the section */
2053 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002054 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002055
Emeric Brun2b920a12010-09-23 18:30:22 +02002056 if (!stopping) {
2057 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002058
2059 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2060 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2061 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002062 /* Resync from local peer needed
2063 no peer was assigned for the lesson
2064 and no old local peer found
2065 or resync timeout expire */
2066
2067 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002068 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002069
2070 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002071 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +02002072 }
2073
2074 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002075 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002076 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002077 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002078 if (!ps->appctx) {
2079 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002080 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002081 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002082 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002083 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002084 tick_is_expired(ps->reconnect, now_ms))) {
2085 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002086 * or previous peer connection established with success
2087 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002088 * and reconnection timer is expired */
2089
2090 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002091 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002092 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002093 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002094 /* If previous session failed during connection
2095 * but reconnection timer is not expired */
2096
2097 /* reschedule task for reconnect */
2098 task->expire = tick_first(task->expire, ps->reconnect);
2099 }
2100 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002101 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002102 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002103 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002104 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2105 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002106 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2107 /* Resync from a remote is needed
2108 * and no peer was assigned for lesson
2109 * and current peer may be up2date */
2110
2111 /* assign peer for the lesson */
2112 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002113 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002114
Willy Tarreau9df94c22016-10-31 18:42:52 +01002115 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002116 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002117 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002118 else {
2119 /* Awake session if there is data to push */
2120 for (st = ps->tables; st ; st = st->next) {
2121 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002122 /* wake up the peer handler to push local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002123 appctx_wakeup(ps->appctx);
2124 break;
2125 }
2126 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002127 }
2128 /* else do nothing */
2129 } /* SUCCESSCODE */
2130 } /* !ps->peer->local */
2131 } /* for */
2132
2133 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002134 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2135 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2136 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002137 /* Resync from remote peer needed
2138 * no peer was assigned for the lesson
2139 * and resync timeout expire */
2140
2141 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002142 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002143 }
2144
Emeric Brunb3971ab2015-05-12 18:49:09 +02002145 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002146 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002147 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2148 if (!tick_is_expired(peers->resync_timeout, now_ms))
2149 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002150 }
2151 } /* !stopping */
2152 else {
2153 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01002154 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08002155 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002156 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002157 /* add DO NOT STOP flag if not present */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002158 HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002159 peers->flags |= PEERS_F_DONOTSTOP;
2160 ps = peers->local;
2161 for (st = ps->tables; st ; st = st->next)
2162 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002163 }
2164
2165 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002166 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun80527f52017-06-19 17:46:37 +02002167 /* we're killing a connection, we must apply a random delay before
2168 * retrying otherwise the other end will do the same and we can loop
2169 * for a while.
2170 */
2171 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau9df94c22016-10-31 18:42:52 +01002172 if (ps->appctx) {
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002173 peer_session_forceshutdown(ps->appctx);
Willy Tarreaue5843b32015-04-27 18:40:14 +02002174 ps->appctx = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002175 }
2176 }
2177 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002178
Emeric Brunb3971ab2015-05-12 18:49:09 +02002179 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002180 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002181 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002182 /* resync of new process was complete, current process can die now */
Willy Tarreaucea85372017-11-29 14:49:30 +01002183 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002184 peers->flags &= ~PEERS_F_DONOTSTOP;
2185 for (st = ps->tables; st ; st = st->next)
2186 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002187 }
2188 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002189 else if (!ps->appctx) {
2190 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002191 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002192 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2193 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2194 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002195 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002196 * or previous peer connection was successfully established
2197 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002198 * or during previous connect, peer replies a try again statuscode */
2199
2200 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002201 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002202 }
2203 else {
2204 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002205 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002206 /* unable to resync new process, current process can die now */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002207 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002208 peers->flags &= ~PEERS_F_DONOTSTOP;
2209 for (st = ps->tables; st ; st = st->next)
2210 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002211 }
2212 }
2213 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002214 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002215 /* current peer connection is active and established
2216 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002217 for (st = ps->tables; st ; st = st->next) {
2218 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002219 appctx_wakeup(ps->appctx);
2220 break;
2221 }
2222 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002223 }
2224 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002225
2226 /* Release lock for all peers of the section */
2227 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002228 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002229
Emeric Brun2b920a12010-09-23 18:30:22 +02002230 /* Wakeup for re-connect */
2231 return task;
2232}
2233
Emeric Brunb3971ab2015-05-12 18:49:09 +02002234
Emeric Brun2b920a12010-09-23 18:30:22 +02002235/*
Willy Tarreaud9443442018-10-15 11:18:03 +02002236 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02002237 */
Willy Tarreaud9443442018-10-15 11:18:03 +02002238int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002239{
Emeric Brun2b920a12010-09-23 18:30:22 +02002240 struct peer * curpeer;
Willy Tarreau4348fad2012-09-20 16:48:07 +02002241 struct listener *listener;
Emeric Brun2b920a12010-09-23 18:30:22 +02002242
Emeric Brun2b920a12010-09-23 18:30:22 +02002243 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002244 peers->peers_fe->maxconn += 3;
2245 }
2246
Willy Tarreau4348fad2012-09-20 16:48:07 +02002247 list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe)
2248 listener->maxconn = peers->peers_fe->maxconn;
Emeric Brunc60def82017-09-27 14:59:38 +02002249 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02002250 if (!peers->sync_task)
2251 return 0;
2252
Emeric Brunb3971ab2015-05-12 18:49:09 +02002253 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002254 peers->sync_task->context = (void *)peers;
2255 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2256 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02002257 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002258}
2259
2260
2261
2262/*
2263 * Function used to register a table for sync on a group of peers
2264 *
2265 */
2266void peers_register_table(struct peers *peers, struct stktable *table)
2267{
2268 struct shared_table *st;
2269 struct peer * curpeer;
2270 int id = 0;
2271
2272 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002273 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002274 st->table = table;
2275 st->next = curpeer->tables;
2276 if (curpeer->tables)
2277 id = curpeer->tables->local_id;
2278 st->local_id = id + 1;
2279
2280 curpeer->tables = st;
2281 }
2282
2283 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002284}
2285